xref: /openbmc/u-boot/disk/part_dos.c (revision ed09a554)
1 /*
2  * (C) Copyright 2001
3  * Raymond Lo, lo@routefree.com
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  *
6  * SPDX-License-Identifier:	GPL-2.0+
7  */
8 
9 /*
10  * Support for harddisk partitions.
11  *
12  * To be compatible with LinuxPPC and Apple we use the standard Apple
13  * SCSI disk partitioning scheme. For more information see:
14  * http://developer.apple.com/techpubs/mac/Devices/Devices-126.html#MARKER-14-92
15  */
16 
17 #include <common.h>
18 #include <command.h>
19 #include <ide.h>
20 #include "part_dos.h"
21 
22 #ifdef HAVE_BLOCK_DEVICE
23 
24 #define DOS_PART_DEFAULT_SECTOR 512
25 
26 /* Convert char[4] in little endian format to the host format integer
27  */
28 static inline int le32_to_int(unsigned char *le32)
29 {
30     return ((le32[3] << 24) +
31 	    (le32[2] << 16) +
32 	    (le32[1] << 8) +
33 	     le32[0]
34 	   );
35 }
36 
37 static inline int is_extended(int part_type)
38 {
39     return (part_type == 0x5 ||
40 	    part_type == 0xf ||
41 	    part_type == 0x85);
42 }
43 
44 static inline int is_bootable(dos_partition_t *p)
45 {
46 	return p->boot_ind == 0x80;
47 }
48 
49 static void print_one_part(dos_partition_t *p, int ext_part_sector,
50 			   int part_num, unsigned int disksig)
51 {
52 	int lba_start = ext_part_sector + le32_to_int (p->start4);
53 	int lba_size  = le32_to_int (p->size4);
54 
55 	printf("%3d\t%-10d\t%-10d\t%08x-%02x\t%02x%s%s\n",
56 		part_num, lba_start, lba_size, disksig, part_num, p->sys_ind,
57 		(is_extended(p->sys_ind) ? " Extd" : ""),
58 		(is_bootable(p) ? " Boot" : ""));
59 }
60 
61 static int test_block_type(unsigned char *buffer)
62 {
63 	int slot;
64 	struct dos_partition *p;
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 	p = (struct dos_partition *)&buffer[DOS_PART_TBL_OFFSET];
71 	for (slot = 0; slot < 3; slot++) {
72 		if (p->boot_ind != 0 && p->boot_ind != 0x80) {
73 			if (!slot &&
74 			    (strncmp((char *)&buffer[DOS_PBR_FSTYPE_OFFSET],
75 				     "FAT", 3) == 0 ||
76 			     strncmp((char *)&buffer[DOS_PBR32_FSTYPE_OFFSET],
77 				     "FAT32", 5) == 0)) {
78 				return DOS_PBR; /* is PBR */
79 			} else {
80 				return -1;
81 			}
82 		}
83 	}
84 	return DOS_MBR;	    /* Is MBR */
85 }
86 
87 
88 int test_part_dos (block_dev_desc_t *dev_desc)
89 {
90 	ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
91 
92 	if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) buffer) != 1)
93 		return -1;
94 
95 	if (test_block_type(buffer) != DOS_MBR)
96 		return -1;
97 
98 	return 0;
99 }
100 
101 /*  Print a partition that is relative to its Extended partition table
102  */
103 static void print_partition_extended(block_dev_desc_t *dev_desc,
104 				     int ext_part_sector, int relative,
105 				     int part_num, unsigned int disksig)
106 {
107 	ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
108 	dos_partition_t *pt;
109 	int i;
110 
111 	if (dev_desc->block_read(dev_desc->dev, ext_part_sector, 1, (ulong *) buffer) != 1) {
112 		printf ("** Can't read partition table on %d:%d **\n",
113 			dev_desc->dev, ext_part_sector);
114 		return;
115 	}
116 	i=test_block_type(buffer);
117 	if (i != DOS_MBR) {
118 		printf ("bad MBR sector signature 0x%02x%02x\n",
119 			buffer[DOS_PART_MAGIC_OFFSET],
120 			buffer[DOS_PART_MAGIC_OFFSET + 1]);
121 		return;
122 	}
123 
124 	if (!ext_part_sector)
125 		disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
126 
127 	/* Print all primary/logical partitions */
128 	pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
129 	for (i = 0; i < 4; i++, pt++) {
130 		/*
131 		 * fdisk does not show the extended partitions that
132 		 * are not in the MBR
133 		 */
134 
135 		if ((pt->sys_ind != 0) &&
136 		    (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) {
137 			print_one_part(pt, ext_part_sector, part_num, disksig);
138 		}
139 
140 		/* Reverse engr the fdisk part# assignment rule! */
141 		if ((ext_part_sector == 0) ||
142 		    (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
143 			part_num++;
144 		}
145 	}
146 
147 	/* Follows the extended partitions */
148 	pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
149 	for (i = 0; i < 4; i++, pt++) {
150 		if (is_extended (pt->sys_ind)) {
151 			int lba_start = le32_to_int (pt->start4) + relative;
152 
153 			print_partition_extended(dev_desc, lba_start,
154 				ext_part_sector == 0  ? lba_start : relative,
155 				part_num, disksig);
156 		}
157 	}
158 
159 	return;
160 }
161 
162 
163 /*  Print a partition that is relative to its Extended partition table
164  */
165 static int get_partition_info_extended (block_dev_desc_t *dev_desc, int ext_part_sector,
166 				 int relative, int part_num,
167 				 int which_part, disk_partition_t *info,
168 				 unsigned int disksig)
169 {
170 	ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
171 	dos_partition_t *pt;
172 	int i;
173 	int dos_type;
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 
188 #ifdef CONFIG_PARTITION_UUIDS
189 	if (!ext_part_sector)
190 		disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
191 #endif
192 
193 	/* Print all primary/logical partitions */
194 	pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
195 	for (i = 0; i < 4; i++, pt++) {
196 		/*
197 		 * fdisk does not show the extended partitions that
198 		 * are not in the MBR
199 		 */
200 		if (((pt->boot_ind & ~0x80) == 0) &&
201 		    (pt->sys_ind != 0) &&
202 		    (part_num == which_part) &&
203 		    (is_extended(pt->sys_ind) == 0)) {
204 			info->blksz = DOS_PART_DEFAULT_SECTOR;
205 			info->start = (lbaint_t)(ext_part_sector +
206 					le32_to_int(pt->start4));
207 			info->size  = (lbaint_t)le32_to_int(pt->size4);
208 			switch(dev_desc->if_type) {
209 				case IF_TYPE_IDE:
210 				case IF_TYPE_SATA:
211 				case IF_TYPE_ATAPI:
212 					sprintf ((char *)info->name, "hd%c%d",
213 						'a' + dev_desc->dev, part_num);
214 					break;
215 				case IF_TYPE_SCSI:
216 					sprintf ((char *)info->name, "sd%c%d",
217 						'a' + dev_desc->dev, part_num);
218 					break;
219 				case IF_TYPE_USB:
220 					sprintf ((char *)info->name, "usbd%c%d",
221 						'a' + dev_desc->dev, part_num);
222 					break;
223 				case IF_TYPE_DOC:
224 					sprintf ((char *)info->name, "docd%c%d",
225 						'a' + dev_desc->dev, part_num);
226 					break;
227 				default:
228 					sprintf ((char *)info->name, "xx%c%d",
229 						'a' + dev_desc->dev, part_num);
230 					break;
231 			}
232 			/* sprintf(info->type, "%d, pt->sys_ind); */
233 			sprintf ((char *)info->type, "U-Boot");
234 			info->bootable = is_bootable(pt);
235 #ifdef CONFIG_PARTITION_UUIDS
236 			sprintf(info->uuid, "%08x-%02x", disksig, part_num);
237 #endif
238 			return 0;
239 		}
240 
241 		/* Reverse engr the fdisk part# assignment rule! */
242 		if ((ext_part_sector == 0) ||
243 		    (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
244 			part_num++;
245 		}
246 	}
247 
248 	/* Follows the extended partitions */
249 	pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
250 	for (i = 0; i < 4; i++, pt++) {
251 		if (is_extended (pt->sys_ind)) {
252 			int lba_start = le32_to_int (pt->start4) + relative;
253 
254 			return get_partition_info_extended (dev_desc, lba_start,
255 				 ext_part_sector == 0 ? lba_start : relative,
256 				 part_num, which_part, info, disksig);
257 		}
258 	}
259 
260 	/* Check for DOS PBR if no partition is found */
261 	dos_type = test_block_type(buffer);
262 
263 	if (dos_type == DOS_PBR) {
264 		info->start = 0;
265 		info->size = dev_desc->lba;
266 		info->blksz = DOS_PART_DEFAULT_SECTOR;
267 		info->bootable = 0;
268 		sprintf ((char *)info->type, "U-Boot");
269 #ifdef CONFIG_PARTITION_UUIDS
270 		info->uuid[0] = 0;
271 #endif
272 		return 0;
273 	}
274 
275 	return -1;
276 }
277 
278 void print_part_dos (block_dev_desc_t *dev_desc)
279 {
280 	printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType\n");
281 	print_partition_extended(dev_desc, 0, 0, 1, 0);
282 }
283 
284 int get_partition_info_dos (block_dev_desc_t *dev_desc, int part, disk_partition_t * info)
285 {
286 	return get_partition_info_extended(dev_desc, 0, 0, 1, part, info, 0);
287 }
288 
289 
290 #endif
291