00001 #include <console/console.h>
00002 #include <fs/fs.h>
00003 #include <arch/io.h>
00004 #include <string.h>
00005 #include <delay.h>
00006 #include <pc80/ide.h>
00007 #include <arch/byteorder.h>
00008
00009 #define NUM_CACHE 64
00010 static unsigned char buf_cache[NUM_CACHE][512];
00011 static unsigned long cache_sect[NUM_CACHE];
00012
00013 static char dev_name[256];
00014
00015 int dev_type = -1;
00016 int dev_drive = -1;
00017 unsigned long part_start;
00018 unsigned long part_length;
00019 int using_devsize;
00020
00021 unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base)
00022 {
00023 unsigned long long result = 0,value;
00024
00025 if (!base) {
00026 base = 10;
00027 if (*cp == '0') {
00028 base = 8;
00029 cp++;
00030 if ((*cp == 'x') && isxdigit(cp[1])) {
00031 cp++;
00032 base = 16;
00033 }
00034 }
00035 }
00036 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
00037 ? toupper(*cp) : *cp)-'A'+10) < base) {
00038 result = result*base + value;
00039 cp++;
00040 }
00041 if (endp)
00042 *endp = (char *)cp;
00043 return result;
00044 }
00045
00046 unsigned long long strtoull_with_suffix(const char *cp,char **endp,unsigned int base)
00047 {
00048 unsigned long long result;
00049
00050 if (!endp) {
00051 return 0;
00052 }
00053 result = simple_strtoull(cp, endp, base);
00054 switch (toupper(**endp)) {
00055 case 'K':
00056 result <<= 10;
00057 ++*endp;
00058 break;
00059 case 'M':
00060 result <<= 20;
00061 ++*endp;
00062 break;
00063 case 'G':
00064 result <<= 30;
00065 ++*endp;
00066 break;
00067 }
00068 return result;
00069 }
00070
00071 unsigned int get_le32(const unsigned char *p)
00072 {
00073 return ((unsigned int) p[0] << 0)
00074 | ((unsigned int) p[1] << 8)
00075 | ((unsigned int) p[2] << 16)
00076 | ((unsigned int) p[3] << 24);
00077 }
00078
00079 static inline int has_pc_part_magic(unsigned char *sect)
00080 {
00081 return sect[510]==0x55 && sect[511]==0xAA;
00082 }
00083
00084 static inline int is_pc_extended_part(unsigned char type)
00085 {
00086 return type==5 || type==0xf || type==0x85;
00087 }
00088
00089
00090 static int open_pc_partition(int part, unsigned long *start_p,
00091 unsigned long *length_p)
00092 {
00093
00094 struct pc_partition {
00095 unsigned char boot;
00096 unsigned char head;
00097 unsigned char sector;
00098 unsigned char cyl;
00099 unsigned char type;
00100 unsigned char e_head;
00101 unsigned char e_sector;
00102 unsigned char e_cyl;
00103 unsigned char start_sect[4];
00104 unsigned char nr_sects[4];
00105 } *p;
00106 unsigned char buf[512];
00107
00108
00109 if (!devread(0, 0, sizeof buf, buf)) {
00110 printk_debug("device read failed\n");
00111 return 0;
00112 }
00113 if (!has_pc_part_magic(buf)) {
00114 printk_debug("pc partition magic number not found\n");
00115
00116 return PARTITION_UNKNOWN;
00117 }
00118 p = (struct pc_partition *) (buf + 0x1be);
00119 if (part < 4) {
00120
00121 p += part;
00122 if (p->type==0 || is_pc_extended_part(p->type)) {
00123 printk_info("Partition %d does not exist\n", part+1);
00124 return 0;
00125 }
00126 *start_p = get_le32(p->start_sect);
00127 *length_p = get_le32(p->nr_sects);
00128 return 1;
00129 } else {
00130
00131 int i;
00132 int cur_part;
00133 unsigned long ext_start, cur_table;
00134
00135
00136 for (i = 0; i < 4; i++) {
00137 if (is_pc_extended_part(p[i].type))
00138 break;
00139 }
00140 if (i >= 4) {
00141 printk_info("Extended partition not found\n");
00142 return 0;
00143 }
00144 printk_debug("Extended partition at %d\n", i+1);
00145
00146 ext_start = get_le32(p[i].start_sect);
00147 cur_table = ext_start;
00148 cur_part = 4;
00149 for (;;) {
00150 printk_debug("cur_part=%d at %lu\n", cur_part, cur_table);
00151 if (!devread(cur_table, 0, sizeof buf, buf))
00152 return 0;
00153 if (!has_pc_part_magic(buf)) {
00154 printk_debug("no magic\n");
00155 break;
00156 }
00157
00158 p = (struct pc_partition *) (buf + 0x1be);
00159
00160 if (cur_part == part) {
00161 if (p->type==0) {
00162 printk_info("Partition %d is empty\n", part+1);
00163 return 0;
00164 }
00165 *start_p = cur_table + get_le32(p->start_sect);
00166 *length_p = get_le32(p->nr_sects);
00167 return 1;
00168 }
00169
00170 if (!is_pc_extended_part(p[1].type)) {
00171 printk_debug("no link\n");
00172 break;
00173 }
00174 cur_table = ext_start + get_le32(p[1].start_sect);
00175
00176 cur_part++;
00177 }
00178 printk_info("Logical partition %d not exist\n", part+1);
00179 return 0;
00180 }
00181 }
00182
00183 static void flush_cache(void)
00184 {
00185 int i;
00186 for (i = 0; i < NUM_CACHE; i++)
00187 cache_sect[i] = (unsigned long) -1;
00188 }
00189
00190 static int parse_device_name(const char *name, int *type, int *drive,
00191 int *part, uint64_t *offset, uint64_t *length)
00192 {
00193 *offset = *length = 0;
00194
00195 if (memcmp(name, "hd", 2) == 0) {
00196 *type = DISK_IDE;
00197 name += 2;
00198 if (*name < 'a' || *name > 'z') {
00199 printk_info("Invalid drive\n");
00200 return 0;
00201 }
00202 *drive = *name - 'a';
00203 name++;
00204 } else if (memcmp(name, "mem", 3) == 0) {
00205 *type = DISK_MEM;
00206 name += 3;
00207 *drive = 0;
00208 } else {
00209 printk_info("Unknown device type\n");
00210 return 0;
00211 }
00212
00213 *part = (int) simple_strtoull(name, (char **)&name, 0);
00214
00215 if (*name == '@') {
00216 name++;
00217 *offset = strtoull_with_suffix(name, (char **)&name, 0);
00218 if (*name == ',')
00219 *length = strtoull_with_suffix(name+1, (char **)&name, 0);
00220 printk_debug("offset=%#Lx length=%#Lx\n", *offset, *length);
00221 }
00222
00223 if (*name != '\0') {
00224 printk_info("Can't parse device name\n");
00225 return 0;
00226 }
00227
00228 return 1;
00229 }
00230
00231 int devopen(const char *name, int *reopen)
00232 {
00233 int type, drive, part, i;
00234 uint64_t offset, length;
00235 uint32_t disk_size = 0;
00236
00237
00238 if (strcmp(name, dev_name) == 0) {
00239 printk_debug("already open\n");
00240 *reopen = 1;
00241 return 1;
00242 }
00243 *reopen = 0;
00244
00245 if (!parse_device_name(name, &type, &drive, &part, &offset, &length)) {
00246 printk_debug("failed to parse device name: %s\n", name);
00247 return 0;
00248 }
00249
00250
00251 if (offset & 0x1ff) {
00252 printk_info("Device offset must be multiple of 512\n");
00253 return 0;
00254 }
00255 if (length & 0x1ff) {
00256 printk_info("WARNING: length is rounded up to multiple of 512\n");
00257 length = (length + 0x1ff) & ~0x1ff;
00258 }
00259
00260 switch (type) {
00261 case DISK_IDE:
00262 printk_debug ("Trying polled ide\n");
00263 printk_debug ("Waiting for ide disks to spin up\n");
00264 printk_notice ("This is a hard coded delay and longer than necessary.\n");
00265 for (i = 0; i < 2; i++) {
00266 printk_notice (".");
00267 delay(1);
00268 }
00269 printk_info ("\n");
00270
00271 if (ide_probe(drive) != 0) {
00272 printk_debug("failed to open ide\n");
00273 return 0;
00274 }
00275 disk_size = (uint32_t) -1;
00276 break;
00277 case DISK_MEM:
00278 disk_size = 1 << (32 - 9);
00279 break;
00280 default:
00281 printk_info("Unknown device type %d\n", type);
00282 return 0;
00283 }
00284
00285 if (dev_type != type || dev_drive != drive)
00286 flush_cache();
00287
00288
00289 dev_type = type;
00290 dev_drive = drive;
00291 part_start = 0;
00292 part_length = disk_size;
00293 using_devsize = 1;
00294
00295 if (part != 0) {
00296
00297 int ret;
00298 ret = open_pc_partition(part - 1, &part_start, &part_length);
00299 if (ret == PARTITION_UNKNOWN) {
00300 ret = open_eltorito_image(part - 1, &part_start, &part_length);
00301 if (ret == PARTITION_UNKNOWN) {
00302 printk_info("Unrecognized partitioning scheme\n");
00303 return 0;
00304 }
00305 }
00306 if (ret == 0) {
00307 printk_debug("can't open partition %d\n", part);
00308 return 0;
00309 }
00310
00311 printk_debug("Partition %d start %lu length %lu\n", part,
00312 part_start, part_length);
00313 }
00314
00315 if (offset) {
00316 if (offset >= (uint64_t) part_length << 9) {
00317 printk_info("Device offset is too high\n");
00318 return 0;
00319 }
00320 part_start += offset >> 9;
00321 part_length -= offset >> 9;
00322 printk_debug("after offset: start %lu, length %lu\n", part_start, part_length);
00323 }
00324
00325 if (length) {
00326 if (length > (uint64_t) part_length << 9) {
00327 printk_info("Specified length exceeds the size of device\n");
00328 return 0;
00329 }
00330 part_length = length >> 9;
00331 printk_debug("after length: length %lu\n", part_length);
00332 using_devsize = 0;
00333 }
00334
00335 strncpy(dev_name, name, sizeof dev_name-1);
00336
00337 return 1;
00338 }
00339
00340
00341 static void *read_sector(unsigned long sector)
00342 {
00343 unsigned int hash;
00344 void *buf;
00345
00346
00347 if (dev_type == DISK_MEM) {
00348 unsigned long phys = sector << 9;
00349
00350 return (void *)phys;
00351 }
00352
00353
00354 hash = sector % NUM_CACHE;
00355 buf = buf_cache[hash];
00356 if (cache_sect[hash] != sector) {
00357 cache_sect[hash] = (unsigned long) -1;
00358 switch (dev_type) {
00359 case DISK_IDE:
00360 if (ide_read(dev_drive, sector, buf) != 0)
00361 goto readerr;
00362 break;
00363 default:
00364 printk_info("read_sector: device not open\n");
00365 return 0;
00366 }
00367 cache_sect[hash] = sector;
00368 }
00369 return buf;
00370
00371 readerr:
00372 printk_info("Disk read error dev=%d drive=%d sector=%lu\n",
00373 dev_type, dev_drive, sector);
00374 dev_name[0] = '\0';
00375 return 0;
00376 }
00377
00378 int devread(unsigned long sector, unsigned long byte_offset,
00379 unsigned long byte_len, void *buf)
00380 {
00381 char *sector_buffer;
00382 char *dest = buf;
00383 unsigned long len;
00384
00385 sector += byte_offset >> 9;
00386 byte_offset &= 0x1ff;
00387
00388 if (sector + ((byte_len + 0x1ff) >> 9) > part_length) {
00389 printk_info("Attempt to read out of device/partition\n");
00390 printk_debug("sector=%lu part_length=%lu byte_len=%lu\n",
00391 sector, part_length, byte_len);
00392 return 0;
00393 }
00394
00395 while (byte_len > 0) {
00396 sector_buffer = read_sector(part_start + sector);
00397 if (!sector_buffer) {
00398 printk_debug("read sector failed\n");
00399 return 0;
00400 }
00401 len = 512 - byte_offset;
00402 if (len > byte_len)
00403 len = byte_len;
00404 memcpy(dest, sector_buffer + byte_offset, len);
00405 sector++;
00406 byte_offset = 0;
00407 byte_len -= len;
00408 dest += len;
00409 }
00410 return 1;
00411 }