part_dos.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. int write_mbr_sector(struct blk_desc *dev_desc, void *buf)
  276. {
  277. if (is_valid_dos_buf(buf))
  278. return -1;
  279. /* write MBR */
  280. if (blk_dwrite(dev_desc, 0, 1, buf) != 1) {
  281. printf("%s: failed writing '%s' (1 blks at 0x0)\n",
  282. __func__, "MBR");
  283. return 1;
  284. }
  285. return 0;
  286. }
  287. U_BOOT_PART_TYPE(dos) = {
  288. .name = "DOS",
  289. .part_type = PART_TYPE_DOS,
  290. .max_entries = DOS_ENTRY_NUMBERS,
  291. .get_info = part_get_info_ptr(part_get_info_dos),
  292. .print = part_print_ptr(part_print_dos),
  293. .test = part_test_dos,
  294. };
  295. #endif