part_dos.c 8.9 KB

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