part_dos.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 ((CONFIG_COMMANDS & CFG_CMD_IDE) || \
  36. (CONFIG_COMMANDS & CFG_CMD_SCSI) || \
  37. (CONFIG_COMMANDS & CFG_CMD_USB) || \
  38. defined(CONFIG_MMC) || \
  39. defined(CONFIG_SYSTEMACE) ) && defined(CONFIG_DOS_PARTITION)
  40. /* Convert char[4] in little endian format to the host format integer
  41. */
  42. static inline int le32_to_int(unsigned char *le32)
  43. {
  44. return ((le32[3] << 24) +
  45. (le32[2] << 16) +
  46. (le32[1] << 8) +
  47. le32[0]
  48. );
  49. }
  50. static inline int is_extended(int part_type)
  51. {
  52. return (part_type == 0x5 ||
  53. part_type == 0xf ||
  54. part_type == 0x85);
  55. }
  56. static void print_one_part (dos_partition_t *p, int ext_part_sector, int part_num)
  57. {
  58. int lba_start = ext_part_sector + le32_to_int (p->start4);
  59. int lba_size = le32_to_int (p->size4);
  60. printf ("%5d\t\t%10d\t%10d\t%2x%s\n",
  61. part_num, lba_start, lba_size, p->sys_ind,
  62. (is_extended (p->sys_ind) ? " Extd" : ""));
  63. }
  64. static int test_block_type(unsigned char *buffer)
  65. {
  66. if((buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) ||
  67. (buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) {
  68. return (-1);
  69. } /* no DOS Signature at all */
  70. if(strncmp(&buffer[DOS_PBR_FSTYPE_OFFSET],"FAT",3)==0)
  71. return DOS_PBR; /* is PBR */
  72. return DOS_MBR; /* Is MBR */
  73. }
  74. int test_part_dos (block_dev_desc_t *dev_desc)
  75. {
  76. unsigned char buffer[DEFAULT_SECTOR_SIZE];
  77. if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) buffer) != 1) ||
  78. (buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) ||
  79. (buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) {
  80. return (-1);
  81. }
  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, int ext_part_sector, int relative,
  87. int part_num)
  88. {
  89. unsigned char buffer[DEFAULT_SECTOR_SIZE];
  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==-1) {
  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(i==DOS_PBR) {
  105. printf (" 1\t\t 0\t%10ld\t%2x\n",
  106. dev_desc->lba, buffer[DOS_PBR_MEDIA_TYPE_OFFSET]);
  107. return;
  108. }
  109. /* Print all primary/logical partitions */
  110. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  111. for (i = 0; i < 4; i++, pt++) {
  112. /*
  113. * fdisk does not show the extended partitions that
  114. * are not in the MBR
  115. */
  116. if ((pt->sys_ind != 0) &&
  117. (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) {
  118. print_one_part (pt, ext_part_sector, part_num);
  119. }
  120. /* Reverse engr the fdisk part# assignment rule! */
  121. if ((ext_part_sector == 0) ||
  122. (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
  123. part_num++;
  124. }
  125. }
  126. /* Follows the extended partitions */
  127. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  128. for (i = 0; i < 4; i++, pt++) {
  129. if (is_extended (pt->sys_ind)) {
  130. int lba_start = le32_to_int (pt->start4) + relative;
  131. print_partition_extended (dev_desc, lba_start,
  132. ext_part_sector == 0 ? lba_start
  133. : relative,
  134. part_num);
  135. }
  136. }
  137. return;
  138. }
  139. /* Print a partition that is relative to its Extended partition table
  140. */
  141. static int get_partition_info_extended (block_dev_desc_t *dev_desc, int ext_part_sector,
  142. int relative, int part_num,
  143. int which_part, disk_partition_t *info)
  144. {
  145. unsigned char buffer[DEFAULT_SECTOR_SIZE];
  146. dos_partition_t *pt;
  147. int i;
  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. /* Print all primary/logical partitions */
  161. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  162. for (i = 0; i < 4; i++, pt++) {
  163. /*
  164. * fdisk does not show the extended partitions that
  165. * are not in the MBR
  166. */
  167. if ((pt->sys_ind != 0) &&
  168. (part_num == which_part) &&
  169. (is_extended(pt->sys_ind) == 0)) {
  170. info->blksz = 512;
  171. info->start = ext_part_sector + le32_to_int (pt->start4);
  172. info->size = le32_to_int (pt->size4);
  173. switch(dev_desc->if_type) {
  174. case IF_TYPE_IDE:
  175. case IF_TYPE_ATAPI:
  176. sprintf (info->name, "hd%c%d\n", 'a' + dev_desc->dev, part_num);
  177. break;
  178. case IF_TYPE_SCSI:
  179. sprintf (info->name, "sd%c%d\n", 'a' + dev_desc->dev, part_num);
  180. break;
  181. case IF_TYPE_USB:
  182. sprintf (info->name, "usbd%c%d\n", 'a' + dev_desc->dev, part_num);
  183. break;
  184. case IF_TYPE_DOC:
  185. sprintf (info->name, "docd%c%d\n", 'a' + dev_desc->dev, part_num);
  186. break;
  187. default:
  188. sprintf (info->name, "xx%c%d\n", 'a' + dev_desc->dev, part_num);
  189. break;
  190. }
  191. /* sprintf(info->type, "%d, pt->sys_ind); */
  192. sprintf (info->type, "U-Boot");
  193. return 0;
  194. }
  195. /* Reverse engr the fdisk part# assignment rule! */
  196. if ((ext_part_sector == 0) ||
  197. (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
  198. part_num++;
  199. }
  200. }
  201. /* Follows the extended partitions */
  202. pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  203. for (i = 0; i < 4; i++, pt++) {
  204. if (is_extended (pt->sys_ind)) {
  205. int lba_start = le32_to_int (pt->start4) + relative;
  206. return get_partition_info_extended (dev_desc, lba_start,
  207. ext_part_sector == 0 ? lba_start : relative,
  208. part_num, which_part, info);
  209. }
  210. }
  211. return -1;
  212. }
  213. void print_part_dos (block_dev_desc_t *dev_desc)
  214. {
  215. printf ("Partition Start Sector Num Sectors Type\n");
  216. print_partition_extended (dev_desc, 0, 0, 1);
  217. }
  218. int get_partition_info_dos (block_dev_desc_t *dev_desc, int part, disk_partition_t * info)
  219. {
  220. return get_partition_info_extended (dev_desc, 0, 0, 1, part, info);
  221. }
  222. #endif /* (CONFIG_COMMANDS & CFG_CMD_IDE) && CONFIG_DOS_PARTITION */