part_dos.c 13 KB

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