blob: 37087a6ac3b2e2ba6d9db2b250ac35d463142db3 [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001/*
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>
wdenkfe8c2802002-11-03 00:38:21 +000036#include "part_dos.h"
37
Stephen Warren2c1af9d2013-02-28 15:03:46 +000038#ifdef HAVE_BLOCK_DEVICE
wdenkfe8c2802002-11-03 00:38:21 +000039
40/* Convert char[4] in little endian format to the host format integer
41 */
42static 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
51static inline int is_extended(int part_type)
52{
53 return (part_type == 0x5 ||
54 part_type == 0xf ||
55 part_type == 0x85);
56}
57
Rob Herring40e0e562012-08-23 11:31:43 +000058static inline int is_bootable(dos_partition_t *p)
59{
60 return p->boot_ind == 0x80;
61}
62
Stephen Warren304b5712012-10-08 08:14:38 +000063static void print_one_part(dos_partition_t *p, int ext_part_sector,
Stephen Warrene2e9b372012-10-08 08:14:40 +000064 int part_num, unsigned int disksig)
wdenkfe8c2802002-11-03 00:38:21 +000065{
66 int lba_start = ext_part_sector + le32_to_int (p->start4);
67 int lba_size = le32_to_int (p->size4);
68
Stephen Warrene2e9b372012-10-08 08:14:40 +000069 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,
Rob Herring40e0e562012-08-23 11:31:43 +000071 (is_extended(p->sys_ind) ? " Extd" : ""),
72 (is_bootable(p) ? " Boot" : ""));
wdenkfe8c2802002-11-03 00:38:21 +000073}
74
wdenk7205e402003-09-10 22:30:53 +000075static int test_block_type(unsigned char *buffer)
76{
77 if((buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) ||
78 (buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) {
79 return (-1);
80 } /* no DOS Signature at all */
Wolfgang Denk66c2d732010-07-19 11:36:57 +020081 if (strncmp((char *)&buffer[DOS_PBR_FSTYPE_OFFSET],"FAT",3)==0 ||
82 strncmp((char *)&buffer[DOS_PBR32_FSTYPE_OFFSET],"FAT32",5)==0) {
wdenk7205e402003-09-10 22:30:53 +000083 return DOS_PBR; /* is PBR */
Wolfgang Denk66c2d732010-07-19 11:36:57 +020084 }
wdenk7205e402003-09-10 22:30:53 +000085 return DOS_MBR; /* Is MBR */
86}
87
wdenkfe8c2802002-11-03 00:38:21 +000088
wdenkfe8c2802002-11-03 00:38:21 +000089int test_part_dos (block_dev_desc_t *dev_desc)
90{
Eric Nelsondec049d2012-03-03 12:02:20 +000091 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenkfe8c2802002-11-03 00:38:21 +000092
Stephen Warrend1efb642012-10-05 13:17:40 +000093 if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) buffer) != 1)
94 return -1;
95
96 if (test_block_type(buffer) != DOS_MBR)
97 return -1;
98
99 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000100}
101
102/* Print a partition that is relative to its Extended partition table
103 */
Stephen Warren304b5712012-10-08 08:14:38 +0000104static void print_partition_extended(block_dev_desc_t *dev_desc,
105 int ext_part_sector, int relative,
Stephen Warrene2e9b372012-10-08 08:14:40 +0000106 int part_num, unsigned int disksig)
wdenkfe8c2802002-11-03 00:38:21 +0000107{
Eric Nelsondec049d2012-03-03 12:02:20 +0000108 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenkfe8c2802002-11-03 00:38:21 +0000109 dos_partition_t *pt;
110 int i;
111
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);
115 return;
116 }
wdenk7205e402003-09-10 22:30:53 +0000117 i=test_block_type(buffer);
Stephen Warrend1efb642012-10-05 13:17:40 +0000118 if (i != DOS_MBR) {
wdenkfe8c2802002-11-03 00:38:21 +0000119 printf ("bad MBR sector signature 0x%02x%02x\n",
120 buffer[DOS_PART_MAGIC_OFFSET],
121 buffer[DOS_PART_MAGIC_OFFSET + 1]);
122 return;
123 }
Stephen Warrend1efb642012-10-05 13:17:40 +0000124
Stephen Warrene2e9b372012-10-08 08:14:40 +0000125 if (!ext_part_sector)
126 disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
127
wdenkfe8c2802002-11-03 00:38:21 +0000128 /* Print all primary/logical partitions */
129 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
130 for (i = 0; i < 4; i++, pt++) {
131 /*
wdenk8bde7f72003-06-27 21:31:46 +0000132 * fdisk does not show the extended partitions that
wdenkfe8c2802002-11-03 00:38:21 +0000133 * are not in the MBR
134 */
135
136 if ((pt->sys_ind != 0) &&
137 (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) {
Stephen Warrene2e9b372012-10-08 08:14:40 +0000138 print_one_part(pt, ext_part_sector, part_num, disksig);
wdenkfe8c2802002-11-03 00:38:21 +0000139 }
140
141 /* Reverse engr the fdisk part# assignment rule! */
142 if ((ext_part_sector == 0) ||
143 (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
144 part_num++;
145 }
146 }
147
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;
153
Stephen Warren304b5712012-10-08 08:14:38 +0000154 print_partition_extended(dev_desc, lba_start,
155 ext_part_sector == 0 ? lba_start : relative,
Stephen Warrene2e9b372012-10-08 08:14:40 +0000156 part_num, disksig);
wdenkfe8c2802002-11-03 00:38:21 +0000157 }
158 }
159
160 return;
161}
162
163
164/* Print a partition that is relative to its Extended partition table
165 */
166static int get_partition_info_extended (block_dev_desc_t *dev_desc, int ext_part_sector,
167 int relative, int part_num,
Stephen Warrend27b5f92012-09-21 09:51:00 +0000168 int which_part, disk_partition_t *info,
169 unsigned int disksig)
wdenkfe8c2802002-11-03 00:38:21 +0000170{
Eric Nelsondec049d2012-03-03 12:02:20 +0000171 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenkfe8c2802002-11-03 00:38:21 +0000172 dos_partition_t *pt;
173 int i;
174
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);
178 return -1;
179 }
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]);
185 return -1;
186 }
187
Stephen Warrend27b5f92012-09-21 09:51:00 +0000188#ifdef CONFIG_PARTITION_UUIDS
189 if (!ext_part_sector)
190 disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
191#endif
192
wdenkfe8c2802002-11-03 00:38:21 +0000193 /* Print all primary/logical partitions */
194 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
195 for (i = 0; i < 4; i++, pt++) {
196 /*
wdenk8bde7f72003-06-27 21:31:46 +0000197 * fdisk does not show the extended partitions that
198 * are not in the MBR
wdenkfe8c2802002-11-03 00:38:21 +0000199 */
Daniel Mack78f4ca72009-09-28 11:40:38 +0200200 if (((pt->boot_ind & ~0x80) == 0) &&
201 (pt->sys_ind != 0) &&
wdenk7f70e852003-05-20 14:25:27 +0000202 (part_num == which_part) &&
203 (is_extended(pt->sys_ind) == 0)) {
wdenkfe8c2802002-11-03 00:38:21 +0000204 info->blksz = 512;
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) {
208 case IF_TYPE_IDE:
Dave Liuc7057b52008-03-26 22:49:44 +0800209 case IF_TYPE_SATA:
wdenkfe8c2802002-11-03 00:38:21 +0000210 case IF_TYPE_ATAPI:
Wolfgang Denk71665522009-07-28 22:35:39 +0200211 sprintf ((char *)info->name, "hd%c%d",
212 'a' + dev_desc->dev, part_num);
wdenkfe8c2802002-11-03 00:38:21 +0000213 break;
214 case IF_TYPE_SCSI:
Wolfgang Denk71665522009-07-28 22:35:39 +0200215 sprintf ((char *)info->name, "sd%c%d",
216 'a' + dev_desc->dev, part_num);
wdenkfe8c2802002-11-03 00:38:21 +0000217 break;
218 case IF_TYPE_USB:
Wolfgang Denk71665522009-07-28 22:35:39 +0200219 sprintf ((char *)info->name, "usbd%c%d",
220 'a' + dev_desc->dev, part_num);
wdenkfe8c2802002-11-03 00:38:21 +0000221 break;
222 case IF_TYPE_DOC:
Wolfgang Denk71665522009-07-28 22:35:39 +0200223 sprintf ((char *)info->name, "docd%c%d",
224 'a' + dev_desc->dev, part_num);
wdenkfe8c2802002-11-03 00:38:21 +0000225 break;
226 default:
Wolfgang Denk71665522009-07-28 22:35:39 +0200227 sprintf ((char *)info->name, "xx%c%d",
228 'a' + dev_desc->dev, part_num);
wdenkfe8c2802002-11-03 00:38:21 +0000229 break;
230 }
231 /* sprintf(info->type, "%d, pt->sys_ind); */
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200232 sprintf ((char *)info->type, "U-Boot");
Rob Herring40e0e562012-08-23 11:31:43 +0000233 info->bootable = is_bootable(pt);
Stephen Warrend27b5f92012-09-21 09:51:00 +0000234#ifdef CONFIG_PARTITION_UUIDS
235 sprintf(info->uuid, "%08x-%02x", disksig, part_num);
236#endif
wdenkfe8c2802002-11-03 00:38:21 +0000237 return 0;
238 }
239
240 /* Reverse engr the fdisk part# assignment rule! */
241 if ((ext_part_sector == 0) ||
242 (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
243 part_num++;
244 }
245 }
246
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;
252
253 return get_partition_info_extended (dev_desc, lba_start,
254 ext_part_sector == 0 ? lba_start : relative,
Stephen Warrend27b5f92012-09-21 09:51:00 +0000255 part_num, which_part, info, disksig);
wdenkfe8c2802002-11-03 00:38:21 +0000256 }
257 }
258 return -1;
259}
260
261void print_part_dos (block_dev_desc_t *dev_desc)
262{
Stephen Warrene2e9b372012-10-08 08:14:40 +0000263 printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType\n");
264 print_partition_extended(dev_desc, 0, 0, 1, 0);
wdenkfe8c2802002-11-03 00:38:21 +0000265}
266
267int get_partition_info_dos (block_dev_desc_t *dev_desc, int part, disk_partition_t * info)
268{
Stephen Warrend27b5f92012-09-21 09:51:00 +0000269 return get_partition_info_extended(dev_desc, 0, 0, 1, part, info, 0);
wdenkfe8c2802002-11-03 00:38:21 +0000270}
271
272
Jon Loeligercde5c642007-07-09 17:22:37 -0500273#endif