part_dos.c 9.0 KB

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