part_dos.c 7.5 KB

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