part_dos.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * (C) Copyright 2001
  3. * Raymond Lo, lo@routefree.com
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. /*
  25. * Support for harddisk partitions.
  26. *
  27. * To be compatible with LinuxPPC and Apple we use the standard Apple
  28. * SCSI disk partitioning scheme. For more information see:
  29. * http://developer.apple.com/techpubs/mac/Devices/Devices-126.html#MARKER-14-92
  30. */
  31. #include <common.h>
  32. #include <command.h>
  33. #include <ide.h>
  34. #include "part_dos.h"
  35. #if defined(CONFIG_CMD_IDE) || \
  36. defined(CONFIG_CMD_SATA) || \
  37. defined(CONFIG_CMD_SCSI) || \
  38. defined(CONFIG_CMD_USB) || \
  39. defined(CONFIG_MMC) || \
  40. defined(CONFIG_SYSTEMACE)
  41. /* Convert char[4] in little endian format to the host format integer
  42. */
  43. static inline int le32_to_int(unsigned char *le32)
  44. {
  45. return ((le32[3] << 24) +
  46. (le32[2] << 16) +
  47. (le32[1] << 8) +
  48. le32[0]
  49. );
  50. }
  51. static inline int is_extended(int part_type)
  52. {
  53. return (part_type == 0x5 ||
  54. part_type == 0xf ||
  55. part_type == 0x85);
  56. }
  57. static void print_one_part (dos_partition_t *p, int ext_part_sector, int part_num)
  58. {
  59. int lba_start = ext_part_sector + le32_to_int (p->start4);
  60. int lba_size = le32_to_int (p->size4);
  61. printf ("%5d\t\t%10d\t%10d\t%2x%s\n",
  62. part_num, lba_start, lba_size, p->sys_ind,
  63. (is_extended (p->sys_ind) ? " Extd" : ""));
  64. }
  65. static int test_block_type(unsigned char *buffer)
  66. {
  67. if((buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) ||
  68. (buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) {
  69. return (-1);
  70. } /* no DOS Signature at all */
  71. if (strncmp((char *)&buffer[DOS_PBR_FSTYPE_OFFSET],"FAT",3)==0 ||
  72. strncmp((char *)&buffer[DOS_PBR32_FSTYPE_OFFSET],"FAT32",5)==0) {
  73. return DOS_PBR; /* is PBR */
  74. }
  75. return DOS_MBR; /* Is MBR */
  76. }
  77. int test_part_dos (block_dev_desc_t *dev_desc)
  78. {
  79. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
  80. if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) buffer) != 1) ||
  81. (buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) ||
  82. (buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) {
  83. return (-1);
  84. }
  85. return (0);
  86. }
  87. /* Print a partition that is relative to its Extended partition table
  88. */
  89. static void print_partition_extended (block_dev_desc_t *dev_desc, int ext_part_sector, int relative,
  90. int part_num)
  91. {
  92. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
  93. dos_partition_t *pt;
  94. int i;
  95. if (dev_desc->block_read(dev_desc->dev, ext_part_sector, 1, (ulong *) buffer) != 1) {
  96. printf ("** Can't read partition table on %d:%d **\n",
  97. dev_desc->dev, ext_part_sector);
  98. return;
  99. }
  100. i=test_block_type(buffer);
  101. if(i==-1) {
  102. printf ("bad MBR sector signature 0x%02x%02x\n",
  103. buffer[DOS_PART_MAGIC_OFFSET],
  104. buffer[DOS_PART_MAGIC_OFFSET + 1]);
  105. return;
  106. }
  107. if(i==DOS_PBR) {
  108. printf (" 1\t\t 0\t%10ld\t%2x\n",
  109. dev_desc->lba, buffer[DOS_PBR_MEDIA_TYPE_OFFSET]);
  110. return;
  111. }
  112. /* Print all primary/logical partitions */
  113. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  114. for (i = 0; i < 4; i++, pt++) {
  115. /*
  116. * fdisk does not show the extended partitions that
  117. * are not in the MBR
  118. */
  119. if ((pt->sys_ind != 0) &&
  120. (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) {
  121. print_one_part (pt, ext_part_sector, part_num);
  122. }
  123. /* Reverse engr the fdisk part# assignment rule! */
  124. if ((ext_part_sector == 0) ||
  125. (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
  126. part_num++;
  127. }
  128. }
  129. /* Follows the extended partitions */
  130. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  131. for (i = 0; i < 4; i++, pt++) {
  132. if (is_extended (pt->sys_ind)) {
  133. int lba_start = le32_to_int (pt->start4) + relative;
  134. print_partition_extended (dev_desc, lba_start,
  135. ext_part_sector == 0 ? lba_start
  136. : relative,
  137. part_num);
  138. }
  139. }
  140. return;
  141. }
  142. /* Print a partition that is relative to its Extended partition table
  143. */
  144. static int get_partition_info_extended (block_dev_desc_t *dev_desc, int ext_part_sector,
  145. int relative, int part_num,
  146. int which_part, disk_partition_t *info)
  147. {
  148. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
  149. dos_partition_t *pt;
  150. int i;
  151. if (dev_desc->block_read (dev_desc->dev, ext_part_sector, 1, (ulong *) buffer) != 1) {
  152. printf ("** Can't read partition table on %d:%d **\n",
  153. dev_desc->dev, ext_part_sector);
  154. return -1;
  155. }
  156. if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
  157. buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
  158. printf ("bad MBR sector signature 0x%02x%02x\n",
  159. buffer[DOS_PART_MAGIC_OFFSET],
  160. buffer[DOS_PART_MAGIC_OFFSET + 1]);
  161. return -1;
  162. }
  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 = 512;
  175. info->start = ext_part_sector + le32_to_int (pt->start4);
  176. info->size = le32_to_int (pt->size4);
  177. switch(dev_desc->if_type) {
  178. case IF_TYPE_IDE:
  179. case IF_TYPE_SATA:
  180. case IF_TYPE_ATAPI:
  181. sprintf ((char *)info->name, "hd%c%d",
  182. 'a' + dev_desc->dev, part_num);
  183. break;
  184. case IF_TYPE_SCSI:
  185. sprintf ((char *)info->name, "sd%c%d",
  186. 'a' + dev_desc->dev, part_num);
  187. break;
  188. case IF_TYPE_USB:
  189. sprintf ((char *)info->name, "usbd%c%d",
  190. 'a' + dev_desc->dev, part_num);
  191. break;
  192. case IF_TYPE_DOC:
  193. sprintf ((char *)info->name, "docd%c%d",
  194. 'a' + dev_desc->dev, part_num);
  195. break;
  196. default:
  197. sprintf ((char *)info->name, "xx%c%d",
  198. 'a' + dev_desc->dev, part_num);
  199. break;
  200. }
  201. /* sprintf(info->type, "%d, pt->sys_ind); */
  202. sprintf ((char *)info->type, "U-Boot");
  203. return 0;
  204. }
  205. /* Reverse engr the fdisk part# assignment rule! */
  206. if ((ext_part_sector == 0) ||
  207. (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
  208. part_num++;
  209. }
  210. }
  211. /* Follows the extended partitions */
  212. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  213. for (i = 0; i < 4; i++, pt++) {
  214. if (is_extended (pt->sys_ind)) {
  215. int lba_start = le32_to_int (pt->start4) + relative;
  216. return get_partition_info_extended (dev_desc, lba_start,
  217. ext_part_sector == 0 ? lba_start : relative,
  218. part_num, which_part, info);
  219. }
  220. }
  221. return -1;
  222. }
  223. void print_part_dos (block_dev_desc_t *dev_desc)
  224. {
  225. printf ("Partition Start Sector Num Sectors Type\n");
  226. print_partition_extended (dev_desc, 0, 0, 1);
  227. }
  228. int get_partition_info_dos (block_dev_desc_t *dev_desc, int part, disk_partition_t * info)
  229. {
  230. return get_partition_info_extended (dev_desc, 0, 0, 1, part, info);
  231. }
  232. #endif