part_dos.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2001
  4. * Raymond Lo, lo@routefree.com
  5. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  6. */
  7. /*
  8. * Support for harddisk partitions.
  9. *
  10. * To be compatible with LinuxPPC and Apple we use the standard Apple
  11. * SCSI disk partitioning scheme. For more information see:
  12. * http://developer.apple.com/techpubs/mac/Devices/Devices-126.html#MARKER-14-92
  13. */
  14. #include <common.h>
  15. #include <blk.h>
  16. #include <command.h>
  17. #include <ide.h>
  18. #include <memalign.h>
  19. #include <asm/unaligned.h>
  20. #include <linux/compiler.h>
  21. #include "part_dos.h"
  22. #include <part.h>
  23. #ifdef CONFIG_HAVE_BLOCK_DEVICE
  24. #define DOS_PART_DEFAULT_SECTOR 512
  25. /* should this be configurable? It looks like it's not very common at all
  26. * to use large numbers of partitions */
  27. #define MAX_EXT_PARTS 256
  28. static inline int is_extended(int part_type)
  29. {
  30. return (part_type == DOS_PART_TYPE_EXTENDED ||
  31. part_type == DOS_PART_TYPE_EXTENDED_LBA ||
  32. part_type == DOS_PART_TYPE_EXTENDED_LINUX);
  33. }
  34. static int get_bootable(dos_partition_t *p)
  35. {
  36. int ret = 0;
  37. if (p->sys_ind == 0xef)
  38. ret |= PART_EFI_SYSTEM_PARTITION;
  39. if (p->boot_ind == 0x80)
  40. ret |= PART_BOOTABLE;
  41. return ret;
  42. }
  43. static void print_one_part(dos_partition_t *p, lbaint_t ext_part_sector,
  44. int part_num, unsigned int disksig)
  45. {
  46. lbaint_t lba_start = ext_part_sector + get_unaligned_le32(p->start4);
  47. lbaint_t lba_size = get_unaligned_le32(p->size4);
  48. printf("%3d\t%-10" LBAFlength "u\t%-10" LBAFlength
  49. "u\t%08x-%02x\t%02x%s%s\n",
  50. part_num, lba_start, lba_size, disksig, part_num, p->sys_ind,
  51. (is_extended(p->sys_ind) ? " Extd" : ""),
  52. (get_bootable(p) ? " Boot" : ""));
  53. }
  54. static int test_block_type(unsigned char *buffer)
  55. {
  56. int slot;
  57. struct dos_partition *p;
  58. int part_count = 0;
  59. if((buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) ||
  60. (buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) {
  61. return (-1);
  62. } /* no DOS Signature at all */
  63. p = (struct dos_partition *)&buffer[DOS_PART_TBL_OFFSET];
  64. /* Check that the boot indicators are valid and count the partitions. */
  65. for (slot = 0; slot < 4; ++slot, ++p) {
  66. if (p->boot_ind != 0 && p->boot_ind != 0x80)
  67. break;
  68. if (p->sys_ind)
  69. ++part_count;
  70. }
  71. /*
  72. * If the partition table is invalid or empty,
  73. * check if this is a DOS PBR
  74. */
  75. if (slot != 4 || !part_count) {
  76. if (!strncmp((char *)&buffer[DOS_PBR_FSTYPE_OFFSET],
  77. "FAT", 3) ||
  78. !strncmp((char *)&buffer[DOS_PBR32_FSTYPE_OFFSET],
  79. "FAT32", 5))
  80. return DOS_PBR; /* This is a DOS PBR and not an MBR */
  81. }
  82. if (slot == 4)
  83. return DOS_MBR; /* This is an DOS MBR */
  84. /* This is neither a DOS MBR nor a DOS PBR */
  85. return -1;
  86. }
  87. static int part_test_dos(struct blk_desc *dev_desc)
  88. {
  89. #ifndef CONFIG_SPL_BUILD
  90. ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, mbr,
  91. DIV_ROUND_UP(dev_desc->blksz, sizeof(legacy_mbr)));
  92. if (blk_dread(dev_desc, 0, 1, (ulong *)mbr) != 1)
  93. return -1;
  94. if (test_block_type((unsigned char *)mbr) != DOS_MBR)
  95. return -1;
  96. if (dev_desc->sig_type == SIG_TYPE_NONE &&
  97. mbr->unique_mbr_signature != 0) {
  98. dev_desc->sig_type = SIG_TYPE_MBR;
  99. dev_desc->mbr_sig = mbr->unique_mbr_signature;
  100. }
  101. #else
  102. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
  103. if (blk_dread(dev_desc, 0, 1, (ulong *)buffer) != 1)
  104. return -1;
  105. if (test_block_type(buffer) != DOS_MBR)
  106. return -1;
  107. #endif
  108. return 0;
  109. }
  110. /* Print a partition that is relative to its Extended partition table
  111. */
  112. static void print_partition_extended(struct blk_desc *dev_desc,
  113. lbaint_t ext_part_sector,
  114. lbaint_t relative,
  115. int part_num, unsigned int disksig)
  116. {
  117. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
  118. dos_partition_t *pt;
  119. int i;
  120. /* set a maximum recursion level */
  121. if (part_num > MAX_EXT_PARTS)
  122. {
  123. printf("** Nested DOS partitions detected, stopping **\n");
  124. return;
  125. }
  126. if (blk_dread(dev_desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
  127. printf ("** Can't read partition table on %d:" LBAFU " **\n",
  128. dev_desc->devnum, ext_part_sector);
  129. return;
  130. }
  131. i=test_block_type(buffer);
  132. if (i != DOS_MBR) {
  133. printf ("bad MBR sector signature 0x%02x%02x\n",
  134. buffer[DOS_PART_MAGIC_OFFSET],
  135. buffer[DOS_PART_MAGIC_OFFSET + 1]);
  136. return;
  137. }
  138. if (!ext_part_sector)
  139. disksig = get_unaligned_le32(&buffer[DOS_PART_DISKSIG_OFFSET]);
  140. /* Print all primary/logical partitions */
  141. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  142. for (i = 0; i < 4; i++, pt++) {
  143. /*
  144. * fdisk does not show the extended partitions that
  145. * are not in the MBR
  146. */
  147. if ((pt->sys_ind != 0) &&
  148. (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) {
  149. print_one_part(pt, ext_part_sector, part_num, disksig);
  150. }
  151. /* Reverse engr the fdisk part# assignment rule! */
  152. if ((ext_part_sector == 0) ||
  153. (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
  154. part_num++;
  155. }
  156. }
  157. /* Follows the extended partitions */
  158. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  159. for (i = 0; i < 4; i++, pt++) {
  160. if (is_extended (pt->sys_ind)) {
  161. lbaint_t lba_start
  162. = get_unaligned_le32 (pt->start4) + relative;
  163. print_partition_extended(dev_desc, lba_start,
  164. ext_part_sector == 0 ? lba_start : relative,
  165. part_num, disksig);
  166. }
  167. }
  168. return;
  169. }
  170. /* Print a partition that is relative to its Extended partition table
  171. */
  172. static int part_get_info_extended(struct blk_desc *dev_desc,
  173. lbaint_t ext_part_sector, lbaint_t relative,
  174. int part_num, int which_part,
  175. struct disk_partition *info, uint disksig)
  176. {
  177. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
  178. dos_partition_t *pt;
  179. int i;
  180. int dos_type;
  181. /* set a maximum recursion level */
  182. if (part_num > MAX_EXT_PARTS)
  183. {
  184. printf("** Nested DOS partitions detected, stopping **\n");
  185. return -1;
  186. }
  187. if (blk_dread(dev_desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
  188. printf ("** Can't read partition table on %d:" LBAFU " **\n",
  189. dev_desc->devnum, ext_part_sector);
  190. return -1;
  191. }
  192. if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
  193. buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
  194. printf ("bad MBR sector signature 0x%02x%02x\n",
  195. buffer[DOS_PART_MAGIC_OFFSET],
  196. buffer[DOS_PART_MAGIC_OFFSET + 1]);
  197. return -1;
  198. }
  199. #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
  200. if (!ext_part_sector)
  201. disksig = get_unaligned_le32(&buffer[DOS_PART_DISKSIG_OFFSET]);
  202. #endif
  203. /* Print all primary/logical partitions */
  204. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  205. for (i = 0; i < 4; i++, pt++) {
  206. /*
  207. * fdisk does not show the extended partitions that
  208. * are not in the MBR
  209. */
  210. if (((pt->boot_ind & ~0x80) == 0) &&
  211. (pt->sys_ind != 0) &&
  212. (part_num == which_part) &&
  213. (ext_part_sector == 0 || is_extended(pt->sys_ind) == 0)) {
  214. info->blksz = DOS_PART_DEFAULT_SECTOR;
  215. info->start = (lbaint_t)(ext_part_sector +
  216. get_unaligned_le32(pt->start4));
  217. info->size = (lbaint_t)get_unaligned_le32(pt->size4);
  218. part_set_generic_name(dev_desc, part_num,
  219. (char *)info->name);
  220. /* sprintf(info->type, "%d, pt->sys_ind); */
  221. strcpy((char *)info->type, "U-Boot");
  222. info->bootable = get_bootable(pt);
  223. #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
  224. sprintf(info->uuid, "%08x-%02x", disksig, part_num);
  225. #endif
  226. info->sys_ind = pt->sys_ind;
  227. return 0;
  228. }
  229. /* Reverse engr the fdisk part# assignment rule! */
  230. if ((ext_part_sector == 0) ||
  231. (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
  232. part_num++;
  233. }
  234. }
  235. /* Follows the extended partitions */
  236. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  237. for (i = 0; i < 4; i++, pt++) {
  238. if (is_extended (pt->sys_ind)) {
  239. lbaint_t lba_start
  240. = get_unaligned_le32 (pt->start4) + relative;
  241. return part_get_info_extended(dev_desc, lba_start,
  242. ext_part_sector == 0 ? lba_start : relative,
  243. part_num, which_part, info, disksig);
  244. }
  245. }
  246. /* Check for DOS PBR if no partition is found */
  247. dos_type = test_block_type(buffer);
  248. if (dos_type == DOS_PBR) {
  249. info->start = 0;
  250. info->size = dev_desc->lba;
  251. info->blksz = DOS_PART_DEFAULT_SECTOR;
  252. info->bootable = 0;
  253. strcpy((char *)info->type, "U-Boot");
  254. #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
  255. info->uuid[0] = 0;
  256. #endif
  257. return 0;
  258. }
  259. return -1;
  260. }
  261. static void __maybe_unused part_print_dos(struct blk_desc *dev_desc)
  262. {
  263. printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType\n");
  264. print_partition_extended(dev_desc, 0, 0, 1, 0);
  265. }
  266. static int __maybe_unused part_get_info_dos(struct blk_desc *dev_desc, int part,
  267. struct disk_partition *info)
  268. {
  269. return part_get_info_extended(dev_desc, 0, 0, 1, part, info, 0);
  270. }
  271. int is_valid_dos_buf(void *buf)
  272. {
  273. return test_block_type(buf) == DOS_MBR ? 0 : -1;
  274. }
  275. #if CONFIG_IS_ENABLED(CMD_MBR)
  276. static void lba_to_chs(lbaint_t lba, unsigned char *rc, unsigned char *rh,
  277. unsigned char *rs)
  278. {
  279. unsigned int c, h, s;
  280. /* use fixed CHS geometry */
  281. unsigned int sectpertrack = 63;
  282. unsigned int heads = 255;
  283. c = (lba + 1) / sectpertrack / heads;
  284. h = (lba + 1) / sectpertrack - c * heads;
  285. s = (lba + 1) - (c * heads + h) * sectpertrack;
  286. if (c > 1023) {
  287. c = 1023;
  288. h = 254;
  289. s = 63;
  290. }
  291. *rc = c & 0xff;
  292. *rh = h;
  293. *rs = s + ((c & 0x300) >> 2);
  294. }
  295. static void mbr_fill_pt_entry(dos_partition_t *pt, lbaint_t start,
  296. lbaint_t relative, lbaint_t size, uchar sys_ind, bool bootable)
  297. {
  298. pt->boot_ind = bootable ? 0x80 : 0x00;
  299. pt->sys_ind = sys_ind;
  300. lba_to_chs(start, &pt->cyl, &pt->head, &pt->sector);
  301. lba_to_chs(start + size - 1, &pt->end_cyl, &pt->end_head, &pt->end_sector);
  302. put_unaligned_le32(relative, &pt->start4);
  303. put_unaligned_le32(size, &pt->size4);
  304. }
  305. int write_mbr_partitions(struct blk_desc *dev,
  306. struct disk_partition *p, int count, unsigned int disksig)
  307. {
  308. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev->blksz);
  309. lbaint_t ext_part_start = 0, ext_part_size = 0, ext_part_sect = 0;
  310. dos_partition_t *pt;
  311. int i;
  312. memset(buffer, 0, dev->blksz);
  313. buffer[DOS_PART_MAGIC_OFFSET] = 0x55;
  314. buffer[DOS_PART_MAGIC_OFFSET + 1] = 0xaa;
  315. put_unaligned_le32(disksig, &buffer[DOS_PART_DISKSIG_OFFSET]);
  316. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  317. /* create all primary partitions */
  318. for (i = 0; i < 4 && i < count; i++, pt++) {
  319. mbr_fill_pt_entry(pt, p[i].start, p[i].start, p[i].size,
  320. p[i].sys_ind, p[i].bootable);
  321. if (is_extended(p[i].sys_ind)) {
  322. ext_part_start = p[i].start;
  323. ext_part_size = p[i].size;
  324. ext_part_sect = p[i].start;
  325. }
  326. }
  327. if (i < count && !ext_part_start) {
  328. printf("%s: extended partition is needed for more than 4 partitions\n",
  329. __func__);
  330. return -1;
  331. }
  332. /* write MBR */
  333. if (blk_dwrite(dev, 0, 1, buffer) != 1) {
  334. printf("%s: failed writing 'MBR' (1 blks at 0x0)\n",
  335. __func__);
  336. return -1;
  337. }
  338. /* create extended volumes */
  339. for (; i < count; i++) {
  340. lbaint_t next_ebr = 0;
  341. memset(buffer, 0, dev->blksz);
  342. buffer[DOS_PART_MAGIC_OFFSET] = 0x55;
  343. buffer[DOS_PART_MAGIC_OFFSET + 1] = 0xaa;
  344. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  345. mbr_fill_pt_entry(pt, p[i].start, p[i].start - ext_part_sect,
  346. p[i].size, p[i].sys_ind, p[i].bootable);
  347. if (i + 1 < count) {
  348. pt++;
  349. next_ebr = p[i].start + p[i].size;
  350. mbr_fill_pt_entry(pt, next_ebr,
  351. next_ebr - ext_part_start,
  352. p[i+1].start + p[i+1].size - next_ebr,
  353. DOS_PART_TYPE_EXTENDED, 0);
  354. }
  355. /* write EBR */
  356. if (blk_dwrite(dev, ext_part_sect, 1, buffer) != 1) {
  357. printf("%s: failed writing 'EBR' (1 blks at 0x%lx)\n",
  358. __func__, ext_part_sect);
  359. return -1;
  360. }
  361. ext_part_sect = next_ebr;
  362. }
  363. /* Update the partition table entries*/
  364. part_init(dev);
  365. return 0;
  366. }
  367. int layout_mbr_partitions(struct disk_partition *p, int count,
  368. lbaint_t total_sectors)
  369. {
  370. struct disk_partition *ext = NULL;
  371. int i, j;
  372. lbaint_t ext_vol_start;
  373. /* calculate primary partitions start and size if needed */
  374. if (!p[0].start)
  375. p[0].start = DOS_PART_DEFAULT_GAP;
  376. for (i = 0; i < 4 && i < count; i++) {
  377. if (!p[i].start)
  378. p[i].start = p[i - 1].start + p[i - 1].size;
  379. if (!p[i].size) {
  380. lbaint_t end = total_sectors;
  381. lbaint_t allocated = 0;
  382. for (j = i + 1; j < 4 && j < count; j++) {
  383. if (p[j].start) {
  384. end = p[j].start;
  385. break;
  386. }
  387. allocated += p[j].size;
  388. }
  389. p[i].size = end - allocated - p[i].start;
  390. }
  391. if (p[i].sys_ind == 0x05)
  392. ext = &p[i];
  393. }
  394. if (i >= 4 && !ext) {
  395. printf("%s: extended partition is needed for more than 4 partitions\n",
  396. __func__);
  397. return -1;
  398. }
  399. /* calculate extended volumes start and size if needed */
  400. ext_vol_start = ext->start;
  401. for (i = 4; i < count; i++) {
  402. if (!p[i].start)
  403. p[i].start = ext_vol_start + DOS_PART_DEFAULT_GAP;
  404. if (!p[i].size) {
  405. lbaint_t end = ext->start + ext->size;
  406. lbaint_t allocated = 0;
  407. for (j = i + 1; j < count; j++) {
  408. if (p[j].start) {
  409. end = p[j].start - DOS_PART_DEFAULT_GAP;
  410. break;
  411. }
  412. allocated += p[j].size + DOS_PART_DEFAULT_GAP;
  413. }
  414. p[i].size = end - allocated - p[i].start;
  415. }
  416. ext_vol_start = p[i].start + p[i].size;
  417. }
  418. return 0;
  419. }
  420. #endif
  421. int write_mbr_sector(struct blk_desc *dev_desc, void *buf)
  422. {
  423. if (is_valid_dos_buf(buf))
  424. return -1;
  425. /* write MBR */
  426. if (blk_dwrite(dev_desc, 0, 1, buf) != 1) {
  427. printf("%s: failed writing '%s' (1 blks at 0x0)\n",
  428. __func__, "MBR");
  429. return 1;
  430. }
  431. /* Update the partition table entries*/
  432. part_init(dev_desc);
  433. return 0;
  434. }
  435. U_BOOT_PART_TYPE(dos) = {
  436. .name = "DOS",
  437. .part_type = PART_TYPE_DOS,
  438. .max_entries = DOS_ENTRY_NUMBERS,
  439. .get_info = part_get_info_ptr(part_get_info_dos),
  440. .print = part_print_ptr(part_print_dos),
  441. .test = part_test_dos,
  442. };
  443. #endif