part_dos.c 8.6 KB

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