part_dos.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * (C) Copyright 2001
  3. * Raymond Lo, lo@routefree.com
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. /*
  9. * Support for harddisk partitions.
  10. *
  11. * To be compatible with LinuxPPC and Apple we use the standard Apple
  12. * SCSI disk partitioning scheme. For more information see:
  13. * http://developer.apple.com/techpubs/mac/Devices/Devices-126.html#MARKER-14-92
  14. */
  15. #include <common.h>
  16. #include <command.h>
  17. #include <ide.h>
  18. #include <memalign.h>
  19. #include "part_dos.h"
  20. #ifdef HAVE_BLOCK_DEVICE
  21. #define DOS_PART_DEFAULT_SECTOR 512
  22. /* Convert char[4] in little endian format to the host format integer
  23. */
  24. static inline unsigned int le32_to_int(unsigned char *le32)
  25. {
  26. return ((le32[3] << 24) +
  27. (le32[2] << 16) +
  28. (le32[1] << 8) +
  29. le32[0]
  30. );
  31. }
  32. static inline int is_extended(int part_type)
  33. {
  34. return (part_type == 0x5 ||
  35. part_type == 0xf ||
  36. part_type == 0x85);
  37. }
  38. static inline int is_bootable(dos_partition_t *p)
  39. {
  40. return (p->sys_ind == 0xef) || (p->boot_ind == 0x80);
  41. }
  42. static void print_one_part(dos_partition_t *p, lbaint_t ext_part_sector,
  43. int part_num, unsigned int disksig)
  44. {
  45. lbaint_t lba_start = ext_part_sector + le32_to_int (p->start4);
  46. lbaint_t lba_size = le32_to_int (p->size4);
  47. printf("%3d\t%-10" LBAFlength "u\t%-10" LBAFlength
  48. "u\t%08x-%02x\t%02x%s%s\n",
  49. part_num, lba_start, lba_size, disksig, part_num, p->sys_ind,
  50. (is_extended(p->sys_ind) ? " Extd" : ""),
  51. (is_bootable(p) ? " Boot" : ""));
  52. }
  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. for (slot = 0; slot < 3; slot++) {
  63. if (p->boot_ind != 0 && p->boot_ind != 0x80) {
  64. if (!slot &&
  65. (strncmp((char *)&buffer[DOS_PBR_FSTYPE_OFFSET],
  66. "FAT", 3) == 0 ||
  67. strncmp((char *)&buffer[DOS_PBR32_FSTYPE_OFFSET],
  68. "FAT32", 5) == 0)) {
  69. return DOS_PBR; /* is PBR */
  70. } else {
  71. return -1;
  72. }
  73. }
  74. }
  75. return DOS_MBR; /* Is MBR */
  76. }
  77. static int part_test_dos(struct blk_desc *dev_desc)
  78. {
  79. #ifndef CONFIG_SPL_BUILD
  80. ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, mbr, dev_desc->blksz);
  81. if (blk_dread(dev_desc, 0, 1, (ulong *)mbr) != 1)
  82. return -1;
  83. if (test_block_type((unsigned char *)mbr) != DOS_MBR)
  84. return -1;
  85. if (dev_desc->sig_type == SIG_TYPE_NONE &&
  86. mbr->unique_mbr_signature != 0) {
  87. dev_desc->sig_type = SIG_TYPE_MBR;
  88. dev_desc->mbr_sig = mbr->unique_mbr_signature;
  89. }
  90. #else
  91. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
  92. if (blk_dread(dev_desc, 0, 1, (ulong *)buffer) != 1)
  93. return -1;
  94. if (test_block_type(buffer) != DOS_MBR)
  95. return -1;
  96. #endif
  97. return 0;
  98. }
  99. /* Print a partition that is relative to its Extended partition table
  100. */
  101. static void print_partition_extended(struct blk_desc *dev_desc,
  102. lbaint_t ext_part_sector,
  103. lbaint_t relative,
  104. int part_num, unsigned int disksig)
  105. {
  106. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
  107. dos_partition_t *pt;
  108. int i;
  109. if (blk_dread(dev_desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
  110. printf ("** Can't read partition table on %d:" LBAFU " **\n",
  111. dev_desc->devnum, ext_part_sector);
  112. return;
  113. }
  114. i=test_block_type(buffer);
  115. if (i != DOS_MBR) {
  116. printf ("bad MBR sector signature 0x%02x%02x\n",
  117. buffer[DOS_PART_MAGIC_OFFSET],
  118. buffer[DOS_PART_MAGIC_OFFSET + 1]);
  119. return;
  120. }
  121. if (!ext_part_sector)
  122. disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
  123. /* Print all primary/logical partitions */
  124. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  125. for (i = 0; i < 4; i++, pt++) {
  126. /*
  127. * fdisk does not show the extended partitions that
  128. * are not in the MBR
  129. */
  130. if ((pt->sys_ind != 0) &&
  131. (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) {
  132. print_one_part(pt, ext_part_sector, part_num, disksig);
  133. }
  134. /* Reverse engr the fdisk part# assignment rule! */
  135. if ((ext_part_sector == 0) ||
  136. (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
  137. part_num++;
  138. }
  139. }
  140. /* Follows the extended partitions */
  141. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  142. for (i = 0; i < 4; i++, pt++) {
  143. if (is_extended (pt->sys_ind)) {
  144. lbaint_t lba_start
  145. = le32_to_int (pt->start4) + relative;
  146. print_partition_extended(dev_desc, lba_start,
  147. ext_part_sector == 0 ? lba_start : relative,
  148. part_num, disksig);
  149. }
  150. }
  151. return;
  152. }
  153. /* Print a partition that is relative to its Extended partition table
  154. */
  155. static int part_get_info_extended(struct blk_desc *dev_desc,
  156. lbaint_t ext_part_sector, lbaint_t relative,
  157. int part_num, int which_part,
  158. disk_partition_t *info, unsigned int disksig)
  159. {
  160. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
  161. dos_partition_t *pt;
  162. int i;
  163. int dos_type;
  164. if (blk_dread(dev_desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
  165. printf ("** Can't read partition table on %d:" LBAFU " **\n",
  166. dev_desc->devnum, ext_part_sector);
  167. return -1;
  168. }
  169. if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
  170. buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
  171. printf ("bad MBR sector signature 0x%02x%02x\n",
  172. buffer[DOS_PART_MAGIC_OFFSET],
  173. buffer[DOS_PART_MAGIC_OFFSET + 1]);
  174. return -1;
  175. }
  176. #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
  177. if (!ext_part_sector)
  178. disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
  179. #endif
  180. /* Print all primary/logical partitions */
  181. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  182. for (i = 0; i < 4; i++, pt++) {
  183. /*
  184. * fdisk does not show the extended partitions that
  185. * are not in the MBR
  186. */
  187. if (((pt->boot_ind & ~0x80) == 0) &&
  188. (pt->sys_ind != 0) &&
  189. (part_num == which_part) &&
  190. (ext_part_sector == 0 || is_extended(pt->sys_ind) == 0)) {
  191. info->blksz = DOS_PART_DEFAULT_SECTOR;
  192. info->start = (lbaint_t)(ext_part_sector +
  193. le32_to_int(pt->start4));
  194. info->size = (lbaint_t)le32_to_int(pt->size4);
  195. part_set_generic_name(dev_desc, part_num,
  196. (char *)info->name);
  197. /* sprintf(info->type, "%d, pt->sys_ind); */
  198. strcpy((char *)info->type, "U-Boot");
  199. info->bootable = is_bootable(pt);
  200. #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
  201. sprintf(info->uuid, "%08x-%02x", disksig, part_num);
  202. #endif
  203. info->sys_ind = pt->sys_ind;
  204. return 0;
  205. }
  206. /* Reverse engr the fdisk part# assignment rule! */
  207. if ((ext_part_sector == 0) ||
  208. (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
  209. part_num++;
  210. }
  211. }
  212. /* Follows the extended partitions */
  213. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  214. for (i = 0; i < 4; i++, pt++) {
  215. if (is_extended (pt->sys_ind)) {
  216. lbaint_t lba_start
  217. = le32_to_int (pt->start4) + relative;
  218. return part_get_info_extended(dev_desc, lba_start,
  219. ext_part_sector == 0 ? lba_start : relative,
  220. part_num, which_part, info, disksig);
  221. }
  222. }
  223. /* Check for DOS PBR if no partition is found */
  224. dos_type = test_block_type(buffer);
  225. if (dos_type == DOS_PBR) {
  226. info->start = 0;
  227. info->size = dev_desc->lba;
  228. info->blksz = DOS_PART_DEFAULT_SECTOR;
  229. info->bootable = 0;
  230. strcpy((char *)info->type, "U-Boot");
  231. #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
  232. info->uuid[0] = 0;
  233. #endif
  234. return 0;
  235. }
  236. return -1;
  237. }
  238. void part_print_dos(struct blk_desc *dev_desc)
  239. {
  240. printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType\n");
  241. print_partition_extended(dev_desc, 0, 0, 1, 0);
  242. }
  243. int part_get_info_dos(struct blk_desc *dev_desc, int part,
  244. disk_partition_t *info)
  245. {
  246. return part_get_info_extended(dev_desc, 0, 0, 1, part, info, 0);
  247. }
  248. int is_valid_dos_buf(void *buf)
  249. {
  250. return test_block_type(buf) == DOS_MBR ? 0 : -1;
  251. }
  252. int write_mbr_partition(struct blk_desc *dev_desc, void *buf)
  253. {
  254. if (is_valid_dos_buf(buf))
  255. return -1;
  256. /* write MBR */
  257. if (blk_dwrite(dev_desc, 0, 1, buf) != 1) {
  258. printf("%s: failed writing '%s' (1 blks at 0x0)\n",
  259. __func__, "MBR");
  260. return 1;
  261. }
  262. return 0;
  263. }
  264. U_BOOT_PART_TYPE(dos) = {
  265. .name = "DOS",
  266. .part_type = PART_TYPE_DOS,
  267. .max_entries = DOS_ENTRY_NUMBERS,
  268. .get_info = part_get_info_ptr(part_get_info_dos),
  269. .print = part_print_ptr(part_print_dos),
  270. .test = part_test_dos,
  271. };
  272. #endif