part_dos.c 8.1 KB

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