part_dos.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 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->boot_ind == 0x80;
  41. }
  42. static void print_one_part(dos_partition_t *p, int ext_part_sector,
  43. int part_num, unsigned int disksig)
  44. {
  45. int lba_start = ext_part_sector + le32_to_int (p->start4);
  46. int lba_size = le32_to_int (p->size4);
  47. printf("%3d\t%-10d\t%-10d\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. int test_part_dos (block_dev_desc_t *dev_desc)
  77. {
  78. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
  79. if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) buffer) != 1)
  80. return -1;
  81. if (test_block_type(buffer) != DOS_MBR)
  82. return -1;
  83. return 0;
  84. }
  85. /* Print a partition that is relative to its Extended partition table
  86. */
  87. static void print_partition_extended(block_dev_desc_t *dev_desc,
  88. int ext_part_sector, int relative,
  89. int part_num, unsigned int disksig)
  90. {
  91. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
  92. dos_partition_t *pt;
  93. int i;
  94. if (dev_desc->block_read(dev_desc->dev, ext_part_sector, 1, (ulong *) buffer) != 1) {
  95. printf ("** Can't read partition table on %d:%d **\n",
  96. dev_desc->dev, ext_part_sector);
  97. return;
  98. }
  99. i=test_block_type(buffer);
  100. if (i != DOS_MBR) {
  101. printf ("bad MBR sector signature 0x%02x%02x\n",
  102. buffer[DOS_PART_MAGIC_OFFSET],
  103. buffer[DOS_PART_MAGIC_OFFSET + 1]);
  104. return;
  105. }
  106. if (!ext_part_sector)
  107. disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
  108. /* Print all primary/logical partitions */
  109. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  110. for (i = 0; i < 4; i++, pt++) {
  111. /*
  112. * fdisk does not show the extended partitions that
  113. * are not in the MBR
  114. */
  115. if ((pt->sys_ind != 0) &&
  116. (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) {
  117. print_one_part(pt, ext_part_sector, part_num, disksig);
  118. }
  119. /* Reverse engr the fdisk part# assignment rule! */
  120. if ((ext_part_sector == 0) ||
  121. (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
  122. part_num++;
  123. }
  124. }
  125. /* Follows the extended partitions */
  126. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  127. for (i = 0; i < 4; i++, pt++) {
  128. if (is_extended (pt->sys_ind)) {
  129. int lba_start = le32_to_int (pt->start4) + relative;
  130. print_partition_extended(dev_desc, lba_start,
  131. ext_part_sector == 0 ? lba_start : relative,
  132. part_num, disksig);
  133. }
  134. }
  135. return;
  136. }
  137. /* Print a partition that is relative to its Extended partition table
  138. */
  139. static int get_partition_info_extended (block_dev_desc_t *dev_desc, int ext_part_sector,
  140. int relative, int part_num,
  141. int which_part, disk_partition_t *info,
  142. unsigned int disksig)
  143. {
  144. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
  145. dos_partition_t *pt;
  146. int i;
  147. int dos_type;
  148. if (dev_desc->block_read (dev_desc->dev, ext_part_sector, 1, (ulong *) buffer) != 1) {
  149. printf ("** Can't read partition table on %d:%d **\n",
  150. dev_desc->dev, ext_part_sector);
  151. return -1;
  152. }
  153. if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
  154. buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
  155. printf ("bad MBR sector signature 0x%02x%02x\n",
  156. buffer[DOS_PART_MAGIC_OFFSET],
  157. buffer[DOS_PART_MAGIC_OFFSET + 1]);
  158. return -1;
  159. }
  160. #ifdef CONFIG_PARTITION_UUIDS
  161. if (!ext_part_sector)
  162. disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
  163. #endif
  164. /* Print all primary/logical partitions */
  165. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  166. for (i = 0; i < 4; i++, pt++) {
  167. /*
  168. * fdisk does not show the extended partitions that
  169. * are not in the MBR
  170. */
  171. if (((pt->boot_ind & ~0x80) == 0) &&
  172. (pt->sys_ind != 0) &&
  173. (part_num == which_part) &&
  174. (is_extended(pt->sys_ind) == 0)) {
  175. info->blksz = DOS_PART_DEFAULT_SECTOR;
  176. info->start = (lbaint_t)(ext_part_sector +
  177. le32_to_int(pt->start4));
  178. info->size = (lbaint_t)le32_to_int(pt->size4);
  179. switch(dev_desc->if_type) {
  180. case IF_TYPE_IDE:
  181. case IF_TYPE_SATA:
  182. case IF_TYPE_ATAPI:
  183. sprintf ((char *)info->name, "hd%c%d",
  184. 'a' + dev_desc->dev, part_num);
  185. break;
  186. case IF_TYPE_SCSI:
  187. sprintf ((char *)info->name, "sd%c%d",
  188. 'a' + dev_desc->dev, part_num);
  189. break;
  190. case IF_TYPE_USB:
  191. sprintf ((char *)info->name, "usbd%c%d",
  192. 'a' + dev_desc->dev, part_num);
  193. break;
  194. case IF_TYPE_DOC:
  195. sprintf ((char *)info->name, "docd%c%d",
  196. 'a' + dev_desc->dev, part_num);
  197. break;
  198. default:
  199. sprintf ((char *)info->name, "xx%c%d",
  200. 'a' + dev_desc->dev, part_num);
  201. break;
  202. }
  203. /* sprintf(info->type, "%d, pt->sys_ind); */
  204. sprintf ((char *)info->type, "U-Boot");
  205. info->bootable = is_bootable(pt);
  206. #ifdef CONFIG_PARTITION_UUIDS
  207. sprintf(info->uuid, "%08x-%02x", disksig, part_num);
  208. #endif
  209. return 0;
  210. }
  211. /* Reverse engr the fdisk part# assignment rule! */
  212. if ((ext_part_sector == 0) ||
  213. (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
  214. part_num++;
  215. }
  216. }
  217. /* Follows the extended partitions */
  218. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  219. for (i = 0; i < 4; i++, pt++) {
  220. if (is_extended (pt->sys_ind)) {
  221. int lba_start = le32_to_int (pt->start4) + relative;
  222. return get_partition_info_extended (dev_desc, lba_start,
  223. ext_part_sector == 0 ? lba_start : relative,
  224. part_num, which_part, info, disksig);
  225. }
  226. }
  227. /* Check for DOS PBR if no partition is found */
  228. dos_type = test_block_type(buffer);
  229. if (dos_type == DOS_PBR) {
  230. info->start = 0;
  231. info->size = dev_desc->lba;
  232. info->blksz = DOS_PART_DEFAULT_SECTOR;
  233. info->bootable = 0;
  234. sprintf ((char *)info->type, "U-Boot");
  235. #ifdef CONFIG_PARTITION_UUIDS
  236. info->uuid[0] = 0;
  237. #endif
  238. return 0;
  239. }
  240. return -1;
  241. }
  242. void print_part_dos (block_dev_desc_t *dev_desc)
  243. {
  244. printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType\n");
  245. print_partition_extended(dev_desc, 0, 0, 1, 0);
  246. }
  247. int get_partition_info_dos (block_dev_desc_t *dev_desc, int part, disk_partition_t * info)
  248. {
  249. return get_partition_info_extended(dev_desc, 0, 0, 1, part, info, 0);
  250. }
  251. #endif