3 * Raymond Lo, lo@routefree.com
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * See file CREDITS for list of people who contributed to this
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.
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.
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,
26 * Support for harddisk partitions.
28 * To be compatible with LinuxPPC and Apple we use the standard Apple
29 * SCSI disk partitioning scheme. For more information see:
30 * http://developer.apple.com/techpubs/mac/Devices/Devices-126.html#MARKER-14-92
38 #ifdef HAVE_BLOCK_DEVICE
40 /* Convert char[4] in little endian format to the host format integer
42 static inline int le32_to_int(unsigned char *le32)
44 return ((le32[3] << 24) +
51 static inline int is_extended(int part_type)
53 return (part_type == 0x5 ||
58 static inline int is_bootable(dos_partition_t *p)
60 return p->boot_ind == 0x80;
63 static void print_one_part(dos_partition_t *p, int ext_part_sector,
64 int part_num, unsigned int disksig)
66 int lba_start = ext_part_sector + le32_to_int (p->start4);
67 int lba_size = le32_to_int (p->size4);
69 printf("%3d\t%-10d\t%-10d\t%08x-%02x\t%02x%s%s\n",
70 part_num, lba_start, lba_size, disksig, part_num, p->sys_ind,
71 (is_extended(p->sys_ind) ? " Extd" : ""),
72 (is_bootable(p) ? " Boot" : ""));
75 static int test_block_type(unsigned char *buffer)
77 if((buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) ||
78 (buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) {
80 } /* no DOS Signature at all */
81 if (strncmp((char *)&buffer[DOS_PBR_FSTYPE_OFFSET],"FAT",3)==0 ||
82 strncmp((char *)&buffer[DOS_PBR32_FSTYPE_OFFSET],"FAT32",5)==0) {
83 return DOS_PBR; /* is PBR */
85 return DOS_MBR; /* Is MBR */
89 int test_part_dos (block_dev_desc_t *dev_desc)
91 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
93 if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) buffer) != 1)
96 if (test_block_type(buffer) != DOS_MBR)
102 /* Print a partition that is relative to its Extended partition table
104 static void print_partition_extended(block_dev_desc_t *dev_desc,
105 int ext_part_sector, int relative,
106 int part_num, unsigned int disksig)
108 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
112 if (dev_desc->block_read(dev_desc->dev, ext_part_sector, 1, (ulong *) buffer) != 1) {
113 printf ("** Can't read partition table on %d:%d **\n",
114 dev_desc->dev, ext_part_sector);
117 i=test_block_type(buffer);
119 printf ("bad MBR sector signature 0x%02x%02x\n",
120 buffer[DOS_PART_MAGIC_OFFSET],
121 buffer[DOS_PART_MAGIC_OFFSET + 1]);
125 if (!ext_part_sector)
126 disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
128 /* Print all primary/logical partitions */
129 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
130 for (i = 0; i < 4; i++, pt++) {
132 * fdisk does not show the extended partitions that
136 if ((pt->sys_ind != 0) &&
137 (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) {
138 print_one_part(pt, ext_part_sector, part_num, disksig);
141 /* Reverse engr the fdisk part# assignment rule! */
142 if ((ext_part_sector == 0) ||
143 (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
148 /* Follows the extended partitions */
149 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
150 for (i = 0; i < 4; i++, pt++) {
151 if (is_extended (pt->sys_ind)) {
152 int lba_start = le32_to_int (pt->start4) + relative;
154 print_partition_extended(dev_desc, lba_start,
155 ext_part_sector == 0 ? lba_start : relative,
164 /* Print a partition that is relative to its Extended partition table
166 static int get_partition_info_extended (block_dev_desc_t *dev_desc, int ext_part_sector,
167 int relative, int part_num,
168 int which_part, disk_partition_t *info,
169 unsigned int disksig)
171 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
175 if (dev_desc->block_read (dev_desc->dev, ext_part_sector, 1, (ulong *) buffer) != 1) {
176 printf ("** Can't read partition table on %d:%d **\n",
177 dev_desc->dev, ext_part_sector);
180 if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
181 buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
182 printf ("bad MBR sector signature 0x%02x%02x\n",
183 buffer[DOS_PART_MAGIC_OFFSET],
184 buffer[DOS_PART_MAGIC_OFFSET + 1]);
188 #ifdef CONFIG_PARTITION_UUIDS
189 if (!ext_part_sector)
190 disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
193 /* Print all primary/logical partitions */
194 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
195 for (i = 0; i < 4; i++, pt++) {
197 * fdisk does not show the extended partitions that
200 if (((pt->boot_ind & ~0x80) == 0) &&
201 (pt->sys_ind != 0) &&
202 (part_num == which_part) &&
203 (is_extended(pt->sys_ind) == 0)) {
205 info->start = ext_part_sector + le32_to_int (pt->start4);
206 info->size = le32_to_int (pt->size4);
207 switch(dev_desc->if_type) {
211 sprintf ((char *)info->name, "hd%c%d",
212 'a' + dev_desc->dev, part_num);
215 sprintf ((char *)info->name, "sd%c%d",
216 'a' + dev_desc->dev, part_num);
219 sprintf ((char *)info->name, "usbd%c%d",
220 'a' + dev_desc->dev, part_num);
223 sprintf ((char *)info->name, "docd%c%d",
224 'a' + dev_desc->dev, part_num);
227 sprintf ((char *)info->name, "xx%c%d",
228 'a' + dev_desc->dev, part_num);
231 /* sprintf(info->type, "%d, pt->sys_ind); */
232 sprintf ((char *)info->type, "U-Boot");
233 info->bootable = is_bootable(pt);
234 #ifdef CONFIG_PARTITION_UUIDS
235 sprintf(info->uuid, "%08x-%02x", disksig, part_num);
240 /* Reverse engr the fdisk part# assignment rule! */
241 if ((ext_part_sector == 0) ||
242 (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
247 /* Follows the extended partitions */
248 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
249 for (i = 0; i < 4; i++, pt++) {
250 if (is_extended (pt->sys_ind)) {
251 int lba_start = le32_to_int (pt->start4) + relative;
253 return get_partition_info_extended (dev_desc, lba_start,
254 ext_part_sector == 0 ? lba_start : relative,
255 part_num, which_part, info, disksig);
261 void print_part_dos (block_dev_desc_t *dev_desc)
263 printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType\n");
264 print_partition_extended(dev_desc, 0, 0, 1, 0);
267 int get_partition_info_dos (block_dev_desc_t *dev_desc, int part, disk_partition_t * info)
269 return get_partition_info_extended(dev_desc, 0, 0, 1, part, info, 0);