part_dos.c 7.1 KB

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