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