part_dos.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 <command.h>
  16. #include <ide.h>
  17. #include <memalign.h>
  18. #include "part_dos.h"
  19. #ifdef CONFIG_HAVE_BLOCK_DEVICE
  20. #define DOS_PART_DEFAULT_SECTOR 512
  21. /* should this be configurable? It looks like it's not very common at all
  22. * to use large numbers of partitions */
  23. #define MAX_EXT_PARTS 256
  24. /* Convert char[4] in little endian format to the host format integer
  25. */
  26. static inline unsigned int le32_to_int(unsigned char *le32)
  27. {
  28. return ((le32[3] << 24) +
  29. (le32[2] << 16) +
  30. (le32[1] << 8) +
  31. le32[0]
  32. );
  33. }
  34. static inline int is_extended(int part_type)
  35. {
  36. return (part_type == 0x5 ||
  37. part_type == 0xf ||
  38. part_type == 0x85);
  39. }
  40. static int get_bootable(dos_partition_t *p)
  41. {
  42. int ret = 0;
  43. if (p->sys_ind == 0xef)
  44. ret |= PART_EFI_SYSTEM_PARTITION;
  45. if (p->boot_ind == 0x80)
  46. ret |= PART_BOOTABLE;
  47. return ret;
  48. }
  49. static void print_one_part(dos_partition_t *p, lbaint_t ext_part_sector,
  50. int part_num, unsigned int disksig)
  51. {
  52. lbaint_t lba_start = ext_part_sector + le32_to_int (p->start4);
  53. lbaint_t lba_size = le32_to_int (p->size4);
  54. printf("%3d\t%-10" LBAFlength "u\t%-10" LBAFlength
  55. "u\t%08x-%02x\t%02x%s%s\n",
  56. part_num, lba_start, lba_size, disksig, part_num, p->sys_ind,
  57. (is_extended(p->sys_ind) ? " Extd" : ""),
  58. (get_bootable(p) ? " Boot" : ""));
  59. }
  60. static int test_block_type(unsigned char *buffer)
  61. {
  62. int slot;
  63. struct dos_partition *p;
  64. int part_count = 0;
  65. if((buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) ||
  66. (buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) {
  67. return (-1);
  68. } /* no DOS Signature at all */
  69. p = (struct dos_partition *)&buffer[DOS_PART_TBL_OFFSET];
  70. /* Check that the boot indicators are valid and count the partitions. */
  71. for (slot = 0; slot < 4; ++slot, ++p) {
  72. if (p->boot_ind != 0 && p->boot_ind != 0x80)
  73. break;
  74. if (p->sys_ind)
  75. ++part_count;
  76. }
  77. /*
  78. * If the partition table is invalid or empty,
  79. * check if this is a DOS PBR
  80. */
  81. if (slot != 4 || !part_count) {
  82. if (!strncmp((char *)&buffer[DOS_PBR_FSTYPE_OFFSET],
  83. "FAT", 3) ||
  84. !strncmp((char *)&buffer[DOS_PBR32_FSTYPE_OFFSET],
  85. "FAT32", 5))
  86. return DOS_PBR; /* This is a DOS PBR and not an MBR */
  87. }
  88. if (slot == 4)
  89. return DOS_MBR; /* This is an DOS MBR */
  90. /* This is neither a DOS MBR nor a DOS PBR */
  91. return -1;
  92. }
  93. static int part_test_dos(struct blk_desc *dev_desc)
  94. {
  95. #ifndef CONFIG_SPL_BUILD
  96. ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, mbr,
  97. DIV_ROUND_UP(dev_desc->blksz, sizeof(legacy_mbr)));
  98. if (blk_dread(dev_desc, 0, 1, (ulong *)mbr) != 1)
  99. return -1;
  100. if (test_block_type((unsigned char *)mbr) != DOS_MBR)
  101. return -1;
  102. if (dev_desc->sig_type == SIG_TYPE_NONE &&
  103. mbr->unique_mbr_signature != 0) {
  104. dev_desc->sig_type = SIG_TYPE_MBR;
  105. dev_desc->mbr_sig = mbr->unique_mbr_signature;
  106. }
  107. #else
  108. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
  109. if (blk_dread(dev_desc, 0, 1, (ulong *)buffer) != 1)
  110. return -1;
  111. if (test_block_type(buffer) != DOS_MBR)
  112. return -1;
  113. #endif
  114. return 0;
  115. }
  116. /* Print a partition that is relative to its Extended partition table
  117. */
  118. static void print_partition_extended(struct blk_desc *dev_desc,
  119. lbaint_t ext_part_sector,
  120. lbaint_t relative,
  121. int part_num, unsigned int disksig)
  122. {
  123. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
  124. dos_partition_t *pt;
  125. int i;
  126. /* set a maximum recursion level */
  127. if (part_num > MAX_EXT_PARTS)
  128. {
  129. printf("** Nested DOS partitions detected, stopping **\n");
  130. return;
  131. }
  132. if (blk_dread(dev_desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
  133. printf ("** Can't read partition table on %d:" LBAFU " **\n",
  134. dev_desc->devnum, ext_part_sector);
  135. return;
  136. }
  137. i=test_block_type(buffer);
  138. if (i != DOS_MBR) {
  139. printf ("bad MBR sector signature 0x%02x%02x\n",
  140. buffer[DOS_PART_MAGIC_OFFSET],
  141. buffer[DOS_PART_MAGIC_OFFSET + 1]);
  142. return;
  143. }
  144. if (!ext_part_sector)
  145. disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
  146. /* Print all primary/logical partitions */
  147. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  148. for (i = 0; i < 4; i++, pt++) {
  149. /*
  150. * fdisk does not show the extended partitions that
  151. * are not in the MBR
  152. */
  153. if ((pt->sys_ind != 0) &&
  154. (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) {
  155. print_one_part(pt, ext_part_sector, part_num, disksig);
  156. }
  157. /* Reverse engr the fdisk part# assignment rule! */
  158. if ((ext_part_sector == 0) ||
  159. (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
  160. part_num++;
  161. }
  162. }
  163. /* Follows the extended partitions */
  164. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  165. for (i = 0; i < 4; i++, pt++) {
  166. if (is_extended (pt->sys_ind)) {
  167. lbaint_t lba_start
  168. = le32_to_int (pt->start4) + relative;
  169. print_partition_extended(dev_desc, lba_start,
  170. ext_part_sector == 0 ? lba_start : relative,
  171. part_num, disksig);
  172. }
  173. }
  174. return;
  175. }
  176. /* Print a partition that is relative to its Extended partition table
  177. */
  178. static int part_get_info_extended(struct blk_desc *dev_desc,
  179. lbaint_t ext_part_sector, lbaint_t relative,
  180. int part_num, int which_part,
  181. disk_partition_t *info, unsigned int disksig)
  182. {
  183. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
  184. dos_partition_t *pt;
  185. int i;
  186. int dos_type;
  187. /* set a maximum recursion level */
  188. if (part_num > MAX_EXT_PARTS)
  189. {
  190. printf("** Nested DOS partitions detected, stopping **\n");
  191. return -1;
  192. }
  193. if (blk_dread(dev_desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
  194. printf ("** Can't read partition table on %d:" LBAFU " **\n",
  195. dev_desc->devnum, ext_part_sector);
  196. return -1;
  197. }
  198. if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
  199. buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
  200. printf ("bad MBR sector signature 0x%02x%02x\n",
  201. buffer[DOS_PART_MAGIC_OFFSET],
  202. buffer[DOS_PART_MAGIC_OFFSET + 1]);
  203. return -1;
  204. }
  205. #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
  206. if (!ext_part_sector)
  207. disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
  208. #endif
  209. /* Print all primary/logical partitions */
  210. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  211. for (i = 0; i < 4; i++, pt++) {
  212. /*
  213. * fdisk does not show the extended partitions that
  214. * are not in the MBR
  215. */
  216. if (((pt->boot_ind & ~0x80) == 0) &&
  217. (pt->sys_ind != 0) &&
  218. (part_num == which_part) &&
  219. (ext_part_sector == 0 || is_extended(pt->sys_ind) == 0)) {
  220. info->blksz = DOS_PART_DEFAULT_SECTOR;
  221. info->start = (lbaint_t)(ext_part_sector +
  222. le32_to_int(pt->start4));
  223. info->size = (lbaint_t)le32_to_int(pt->size4);
  224. part_set_generic_name(dev_desc, part_num,
  225. (char *)info->name);
  226. /* sprintf(info->type, "%d, pt->sys_ind); */
  227. strcpy((char *)info->type, "U-Boot");
  228. info->bootable = get_bootable(pt);
  229. #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
  230. sprintf(info->uuid, "%08x-%02x", disksig, part_num);
  231. #endif
  232. info->sys_ind = pt->sys_ind;
  233. return 0;
  234. }
  235. /* Reverse engr the fdisk part# assignment rule! */
  236. if ((ext_part_sector == 0) ||
  237. (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
  238. part_num++;
  239. }
  240. }
  241. /* Follows the extended partitions */
  242. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  243. for (i = 0; i < 4; i++, pt++) {
  244. if (is_extended (pt->sys_ind)) {
  245. lbaint_t lba_start
  246. = le32_to_int (pt->start4) + relative;
  247. return part_get_info_extended(dev_desc, lba_start,
  248. ext_part_sector == 0 ? lba_start : relative,
  249. part_num, which_part, info, disksig);
  250. }
  251. }
  252. /* Check for DOS PBR if no partition is found */
  253. dos_type = test_block_type(buffer);
  254. if (dos_type == DOS_PBR) {
  255. info->start = 0;
  256. info->size = dev_desc->lba;
  257. info->blksz = DOS_PART_DEFAULT_SECTOR;
  258. info->bootable = 0;
  259. strcpy((char *)info->type, "U-Boot");
  260. #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
  261. info->uuid[0] = 0;
  262. #endif
  263. return 0;
  264. }
  265. return -1;
  266. }
  267. void part_print_dos(struct blk_desc *dev_desc)
  268. {
  269. printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType\n");
  270. print_partition_extended(dev_desc, 0, 0, 1, 0);
  271. }
  272. int part_get_info_dos(struct blk_desc *dev_desc, int part,
  273. disk_partition_t *info)
  274. {
  275. return part_get_info_extended(dev_desc, 0, 0, 1, part, info, 0);
  276. }
  277. int is_valid_dos_buf(void *buf)
  278. {
  279. return test_block_type(buf) == DOS_MBR ? 0 : -1;
  280. }
  281. int write_mbr_partition(struct blk_desc *dev_desc, void *buf)
  282. {
  283. if (is_valid_dos_buf(buf))
  284. return -1;
  285. /* write MBR */
  286. if (blk_dwrite(dev_desc, 0, 1, buf) != 1) {
  287. printf("%s: failed writing '%s' (1 blks at 0x0)\n",
  288. __func__, "MBR");
  289. return 1;
  290. }
  291. return 0;
  292. }
  293. U_BOOT_PART_TYPE(dos) = {
  294. .name = "DOS",
  295. .part_type = PART_TYPE_DOS,
  296. .max_entries = DOS_ENTRY_NUMBERS,
  297. .get_info = part_get_info_ptr(part_get_info_dos),
  298. .print = part_print_ptr(part_print_dos),
  299. .test = part_test_dos,
  300. };
  301. #endif