You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. #include <etherboot.h>
  2. #include <lib.h>
  3. #include <fs.h>
  4. #define DEBUG_THIS DEBUG_BLOCKDEV
  5. #include <debug.h>
  6. #define NUM_CACHE 64
  7. static unsigned char buf_cache[NUM_CACHE][512];
  8. static unsigned long cache_sect[NUM_CACHE];
  9. static char dev_name[256];
  10. int dev_type = -1;
  11. int dev_drive = -1;
  12. unsigned long part_start;
  13. unsigned long part_length;
  14. int using_devsize;
  15. static inline int has_pc_part_magic(unsigned char *sect)
  16. {
  17. return sect[510]==0x55 && sect[511]==0xAA;
  18. }
  19. static inline int is_pc_extended_part(unsigned char type)
  20. {
  21. return type==5 || type==0xf || type==0x85;
  22. }
  23. /* IBM-PC/MS-DOS style partitioning scheme */
  24. static int open_pc_partition(int part, unsigned long *start_p,
  25. unsigned long *length_p)
  26. {
  27. /* Layout of PC partition table */
  28. struct pc_partition {
  29. unsigned char boot;
  30. unsigned char head;
  31. unsigned char sector;
  32. unsigned char cyl;
  33. unsigned char type;
  34. unsigned char e_head;
  35. unsigned char e_sector;
  36. unsigned char e_cyl;
  37. unsigned char start_sect[4]; /* unaligned little endian */
  38. unsigned char nr_sects[4]; /* ditto */
  39. } *p;
  40. unsigned char buf[512];
  41. /* PC partition probe */
  42. if (!devread(0, 0, sizeof(buf), buf)) {
  43. debug("device read failed\n");
  44. return 0;
  45. }
  46. if (!has_pc_part_magic(buf)) {
  47. debug("pc partition magic number not found\n");
  48. //debug_hexdump(buf, 512);
  49. return PARTITION_UNKNOWN;
  50. }
  51. p = (struct pc_partition *) (buf + 0x1be);
  52. if (part < 4) {
  53. /* Primary partition */
  54. p += part;
  55. if (p->type==0 || is_pc_extended_part(p->type)) {
  56. printf("Partition %d does not exist\n", part+1);
  57. return 0;
  58. }
  59. *start_p = get_le32(p->start_sect);
  60. *length_p = get_le32(p->nr_sects);
  61. return 1;
  62. } else {
  63. /* Extended partition */
  64. int i;
  65. int cur_part;
  66. unsigned long ext_start, cur_table;
  67. /* Search for the extended partition
  68. * which contains logical partitions */
  69. for (i = 0; i < 4; i++) {
  70. if (is_pc_extended_part(p[i].type))
  71. break;
  72. }
  73. if (i >= 4) {
  74. printf("Extended partition not found\n");
  75. return 0;
  76. }
  77. debug("Extended partition at %d\n", i+1);
  78. /* Visit each logical partition labels */
  79. ext_start = get_le32(p[i].start_sect);
  80. cur_table = ext_start;
  81. cur_part = 4;
  82. for (;;) {
  83. debug("cur_part=%d at %lu\n", cur_part, cur_table);
  84. if (!devread(cur_table, 0, sizeof(buf), buf))
  85. return 0;
  86. if (!has_pc_part_magic(buf)) {
  87. debug("no magic\n");
  88. break;
  89. }
  90. p = (struct pc_partition *) (buf + 0x1be);
  91. /* First entry is the logical partition */
  92. if (cur_part == part) {
  93. if (p->type==0) {
  94. printf("Partition %d is empty\n", part+1);
  95. return 0;
  96. }
  97. *start_p = cur_table + get_le32(p->start_sect);
  98. *length_p = get_le32(p->nr_sects);
  99. return 1;
  100. }
  101. /* Second entry is link to next partition */
  102. if (!is_pc_extended_part(p[1].type)) {
  103. debug("no link\n");
  104. break;
  105. }
  106. cur_table = ext_start + get_le32(p[1].start_sect);
  107. cur_part++;
  108. }
  109. printf("Logical partition %d not exist\n", part+1);
  110. return 0;
  111. }
  112. }
  113. static void flush_cache(void)
  114. {
  115. int i;
  116. for (i = 0; i < NUM_CACHE; i++)
  117. cache_sect[i] = (unsigned long) -1;
  118. }
  119. static int parse_device_name(const char *name, int *type, int *drive,
  120. int *part, uint64_t *offset, uint64_t *length)
  121. {
  122. *offset = *length = 0;
  123. if (memcmp(name, "hd", 2) == 0) {
  124. *type = DISK_IDE;
  125. name += 2;
  126. if (*name < 'a' || *name > 'z') {
  127. printf("Invalid drive\n");
  128. return 0;
  129. }
  130. *drive = *name - 'a';
  131. name++;
  132. } else if (memcmp(name, "mem", 3) == 0) {
  133. *type = DISK_MEM;
  134. name += 3;
  135. *drive = 0;
  136. } else if (memcmp(name, "ud", 2) == 0) {
  137. *type = DISK_USB;
  138. name += 2;
  139. if (*name < 'a' || *name > 'z') {
  140. printf("Invalid drive\n");
  141. return 0;
  142. }
  143. *drive = *name - 'a';
  144. name++;
  145. } else {
  146. printf("Unknown device type\n");
  147. return 0;
  148. }
  149. *part = (int) simple_strtoull(name, (char **)&name, 0);
  150. if (*name == '@') {
  151. name++;
  152. *offset = strtoull_with_suffix(name, (char **)&name, 0);
  153. if (*name == ',')
  154. *length = strtoull_with_suffix(name+1, (char **)&name, 0);
  155. // debug("offset=%#Lx length=%#Lx\n", *offset, *length);
  156. }
  157. if (*name != '\0') {
  158. printf("Can't parse device name\n");
  159. return 0;
  160. }
  161. return 1;
  162. }
  163. int devopen(const char *name, int *reopen)
  164. {
  165. int type, drive, part;
  166. uint64_t offset, length;
  167. uint32_t disk_size = 0;
  168. /* Don't re-open the device that's already open */
  169. if (strcmp(name, dev_name) == 0) {
  170. debug("already open\n");
  171. *reopen = 1;
  172. return 1;
  173. }
  174. *reopen = 0;
  175. if (!parse_device_name(name, &type, &drive, &part, &offset, &length)) {
  176. debug("failed to parse device name: %s\n", name);
  177. return 0;
  178. }
  179. /* Do simple sanity check first */
  180. if (offset & 0x1ff) {
  181. printf("Device offset must be multiple of 512\n");
  182. return 0;
  183. }
  184. if (length & 0x1ff) {
  185. debugx("WARNING: length is rounded up to multiple of 512\n");
  186. length = (length + 0x1ff) & ~0x1ff;
  187. }
  188. switch (type) {
  189. #ifdef IDE_DISK
  190. case DISK_IDE:
  191. if (ide_probe(drive) != 0) {
  192. debug("failed to open ide\n");
  193. return 0;
  194. }
  195. disk_size = (uint32_t) -1; /* FIXME */
  196. break;
  197. #endif
  198. case DISK_MEM:
  199. disk_size = 1 << (32 - 9); /* 4GB/512-byte */
  200. break;
  201. #ifdef USB_DISK
  202. case DISK_USB:
  203. if (usb_probe(drive) != 0) {
  204. debug("failed to open usb\n");
  205. return 0;
  206. }
  207. disk_size = (uint32_t) -1; /* FIXME */
  208. break;
  209. #endif
  210. default:
  211. printf("Unknown device type %d\n", type);
  212. return 0;
  213. }
  214. if (dev_type != type || dev_drive != drive)
  215. flush_cache();
  216. /* start with whole disk */
  217. dev_type = type;
  218. dev_drive = drive;
  219. part_start = 0;
  220. part_length = disk_size;
  221. using_devsize = 1;
  222. if (part != 0) {
  223. /* partition is specified */
  224. int ret;
  225. ret = open_pc_partition(part - 1, &part_start, &part_length);
  226. if (ret == PARTITION_UNKNOWN) {
  227. ret = open_eltorito_image(part - 1, &part_start, &part_length);
  228. if (ret == PARTITION_UNKNOWN) {
  229. printf("Unrecognized partitioning scheme\n");
  230. return 0;
  231. }
  232. }
  233. if (ret == 0) {
  234. debug("can't open partition %d\n", part);
  235. return 0;
  236. }
  237. debug("Partition %d start %lu length %lu\n", part,
  238. part_start, part_length);
  239. }
  240. if (offset) {
  241. if (offset >= (uint64_t) part_length << 9) {
  242. printf("Device offset is too high\n");
  243. return 0;
  244. }
  245. part_start += offset >> 9;
  246. part_length -= offset >> 9;
  247. debug("after offset: start %lu, length %lu\n", part_start, part_length);
  248. }
  249. if (length) {
  250. if (length > (uint64_t) part_length << 9) {
  251. printf("Specified length exceeds the size of device\n");
  252. return 0;
  253. }
  254. part_length = length >> 9;
  255. debug("after length: length %lu\n", part_length);
  256. using_devsize = 0;
  257. }
  258. strncpy(dev_name, name, sizeof(dev_name)-1);
  259. return 1;
  260. }
  261. /* Read a sector from opened device with simple/stupid buffer cache */
  262. static void *read_sector(unsigned long sector)
  263. {
  264. unsigned int hash;
  265. void *buf;
  266. int i;
  267. /* If reading memory, just return the memory as the buffer */
  268. if (dev_type == DISK_MEM) {
  269. unsigned long phys = sector << 9;
  270. //debug("mem: %#lx\n", phys);
  271. return phys_to_virt(phys);
  272. }
  273. /* Search in the cache */
  274. hash = sector % NUM_CACHE;
  275. buf = buf_cache[hash];
  276. if (cache_sect[hash] != sector) {
  277. cache_sect[hash] = (unsigned long) -1;
  278. switch (dev_type) {
  279. #ifdef IDE_DISK
  280. case DISK_IDE:
  281. if (ide_read(dev_drive, sector, buf) != 0)
  282. goto readerr;
  283. break;
  284. #endif
  285. #ifdef USB_DISK
  286. case DISK_USB:
  287. if (usb_read(dev_drive, sector, buf) != 0)
  288. goto readerr;
  289. break;
  290. #endif
  291. default:
  292. printf("read_sector: device not open\n");
  293. return 0;
  294. }
  295. cache_sect[hash] = sector;
  296. }
  297. #if 0
  298. printf("in read_sector:\n");
  299. for(i=0;i<128;i++) {
  300. if((i%4)==0) printf("\n %08x:",i*4);
  301. printf(" %08x ",(uint32_t)*((uint32_t *)buf+i));
  302. }
  303. #endif
  304. return buf;
  305. readerr:
  306. printf("Disk read error dev_type=%d drive=%d sector=%x\n",
  307. dev_type, dev_drive, sector);
  308. dev_name[0] = '\0'; /* force re-open the device next time */
  309. return 0;
  310. }
  311. int devread(unsigned long sector, unsigned long byte_offset,
  312. unsigned long byte_len, void *buf)
  313. {
  314. char *sector_buffer;
  315. char *dest = buf;
  316. unsigned long len;
  317. int i;
  318. sector += byte_offset >> 9;
  319. byte_offset &= 0x1ff;
  320. if (sector + ((byte_len + 0x1ff) >> 9) > part_length) {
  321. printf("Attempt to read out of device/partition\n");
  322. debug("sector=%x part_length=%x byte_len=%x\n",
  323. sector, part_length, byte_len);
  324. return 0;
  325. }
  326. while (byte_len > 0) {
  327. sector_buffer = read_sector(part_start + sector);
  328. if (!sector_buffer) {
  329. debug("read sector failed\n");
  330. return 0;
  331. }
  332. len = 512 - byte_offset;
  333. if (len > byte_len)
  334. len = byte_len;
  335. memcpy(dest, sector_buffer + byte_offset, len);
  336. sector++;
  337. byte_offset = 0;
  338. byte_len -= len;
  339. dest += len;
  340. }
  341. return 1;
  342. }