part_dos.c 8.1 KB

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