part_dos.c 9.1 KB

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