xref: /openbmc/u-boot/disk/part_iso.c (revision 522de019)
1 /*
2  * (C) Copyright 2001
3  * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23 
24 #include <common.h>
25 #include <command.h>
26 #include "part_iso.h"
27 
28 #if defined(CONFIG_CMD_IDE) || \
29     defined(CONFIG_CMD_SCSI) || \
30     defined(CONFIG_CMD_SATA) || \
31     defined(CONFIG_CMD_USB) || \
32     defined(CONFIG_MMC) || \
33     defined(CONFIG_SYSTEMACE)
34 
35 /* #define	ISO_PART_DEBUG */
36 
37 #ifdef	ISO_PART_DEBUG
38 #define	PRINTF(fmt,args...)	printf (fmt ,##args)
39 #else
40 #define PRINTF(fmt,args...)
41 #endif
42 
43 /* enable this if CDs are written with the PowerPC Platform ID */
44 #undef CHECK_FOR_POWERPC_PLATTFORM
45 #define CD_SECTSIZE 2048
46 
47 static unsigned char tmpbuf[CD_SECTSIZE];
48 
49 /* Convert char[4] in little endian format to the host format integer
50  */
51 static inline unsigned long le32_to_int(unsigned char *le32)
52 {
53     return ((le32[3] << 24) +
54 	    (le32[2] << 16) +
55 	    (le32[1] << 8) +
56 	     le32[0]
57 	   );
58 }
59 /* Convert char[2] in little endian format to the host format integer
60  */
61 static inline unsigned short le16_to_int(unsigned char *le16)
62 {
63     return ((le16[1] << 8) +
64 	   le16[0]
65 	   );
66 }
67 
68 
69 /* only boot records will be listed as valid partitions */
70 int get_partition_info_iso_verb(block_dev_desc_t * dev_desc, int part_num, disk_partition_t * info, int verb)
71 {
72 	int i,offset,entry_num;
73 	unsigned short *chksumbuf;
74 	unsigned short chksum;
75 	unsigned long newblkaddr,blkaddr,lastsect,bootaddr;
76 	iso_boot_rec_t *pbr = (iso_boot_rec_t	*)tmpbuf; /* boot record */
77 	iso_pri_rec_t *ppr = (iso_pri_rec_t	*)tmpbuf;	/* primary desc */
78 	iso_val_entry_t *pve = (iso_val_entry_t *)tmpbuf;
79 	iso_init_def_entry_t *pide;
80 
81 	/* the first sector (sector 0x10) must be a primary volume desc */
82 	blkaddr=PVD_OFFSET;
83 	if (dev_desc->block_read (dev_desc->dev, PVD_OFFSET, 1, (ulong *) tmpbuf) != 1)
84 	return (-1);
85 	if(ppr->desctype!=0x01) {
86 		if(verb)
87 			printf ("** First descriptor is NOT a primary desc on %d:%d **\n",
88 				dev_desc->dev, part_num);
89 		return (-1);
90 	}
91 	if(strncmp((char *)ppr->stand_ident,"CD001",5)!=0) {
92 		if(verb)
93 			printf ("** Wrong ISO Ident: %s on %d:%d **\n",
94 				ppr->stand_ident,dev_desc->dev, part_num);
95 		return (-1);
96 	}
97 	lastsect= ((ppr->firstsek_LEpathtab1_LE & 0x000000ff)<<24) +
98 		  ((ppr->firstsek_LEpathtab1_LE & 0x0000ff00)<< 8) +
99 		  ((ppr->firstsek_LEpathtab1_LE & 0x00ff0000)>> 8) +
100 		  ((ppr->firstsek_LEpathtab1_LE & 0xff000000)>>24) ;
101 	info->blksz=ppr->secsize_BE; /* assuming same block size for all entries */
102 	PRINTF(" Lastsect:%08lx\n",lastsect);
103 	for(i=blkaddr;i<lastsect;i++) {
104 		PRINTF("Reading block %d\n", i);
105 		if (dev_desc->block_read (dev_desc->dev, i, 1, (ulong *) tmpbuf) != 1)
106 		return (-1);
107 		if(ppr->desctype==0x00)
108 			break; /* boot entry found */
109 		if(ppr->desctype==0xff) {
110 			if(verb)
111 				printf ("** No valid boot catalog found on %d:%d **\n",
112 					dev_desc->dev, part_num);
113 			return (-1);
114 		}
115 	}
116 	/* boot entry found */
117 	if(strncmp(pbr->ident_str,"EL TORITO SPECIFICATION",23)!=0) {
118 		if(verb)
119 			printf ("** Wrong El Torito ident: %s on %d:%d **\n",
120 				pbr->ident_str,dev_desc->dev, part_num);
121 		return (-1);
122 	}
123 	bootaddr=le32_to_int(pbr->pointer);
124 	PRINTF(" Boot Entry at: %08lX\n",bootaddr);
125 	if (dev_desc->block_read (dev_desc->dev, bootaddr, 1, (ulong *) tmpbuf) != 1) {
126 		if(verb)
127 			printf ("** Can't read Boot Entry at %lX on %d:%d **\n",
128 				bootaddr,dev_desc->dev, part_num);
129 		return (-1);
130 	}
131 	chksum=0;
132 	chksumbuf = (unsigned short *)tmpbuf;
133 	for(i=0;i<0x10;i++)
134 		chksum+=((chksumbuf[i] &0xff)<<8)+((chksumbuf[i] &0xff00)>>8);
135 	if(chksum!=0) {
136 		if(verb)
137 			printf ("** Checksum Error in booting catalog validation entry on %d:%d **\n",
138 				dev_desc->dev, part_num);
139 		return (-1);
140 	}
141 	if((pve->key[0]!=0x55)||(pve->key[1]!=0xAA)) {
142 		if(verb)
143 			printf ("** Key 0x55 0xAA error on %d:%d **\n",
144 				dev_desc->dev, part_num);
145 		return(-1);
146 	}
147 #ifdef CHECK_FOR_POWERPC_PLATTFORM
148 	if(pve->platform!=0x01) {
149 		if(verb)
150 			printf ("** No PowerPC platform CD on %d:%d **\n",
151 				dev_desc->dev, part_num);
152 		return(-1);
153 	}
154 #endif
155 	/* the validation entry seems to be ok, now search the "partition" */
156 	entry_num=0;
157 	offset=0x20;
158 	sprintf ((char *)info->type, "U-Boot");
159 	switch(dev_desc->if_type) {
160 		case IF_TYPE_IDE:
161 		case IF_TYPE_SATA:
162 		case IF_TYPE_ATAPI:
163 			sprintf ((char *)info->name, "hd%c%d",
164 				'a' + dev_desc->dev, part_num);
165 			break;
166 		case IF_TYPE_SCSI:
167 			sprintf ((char *)info->name, "sd%c%d",
168 				'a' + dev_desc->dev, part_num);
169 			break;
170 		case IF_TYPE_USB:
171 			sprintf ((char *)info->name, "usbd%c%d",
172 				'a' + dev_desc->dev, part_num);
173 			break;
174 		case IF_TYPE_DOC:
175 			sprintf ((char *)info->name, "docd%c%d",
176 				'a' + dev_desc->dev, part_num);
177 			break;
178 		default:
179 			sprintf ((char *)info->name, "xx%c%d",
180 				'a' + dev_desc->dev, part_num);
181 			break;
182 	}
183 	/* the bootcatalog (including validation Entry) is limited to 2048Bytes
184 	 * (63 boot entries + validation entry) */
185 	 while(offset<2048) {
186 		pide=(iso_init_def_entry_t *)&tmpbuf[offset];
187 		if ((pide->boot_ind==0x88) ||
188 		    (pide->boot_ind==0x00)) { /* Header Id for default Sections Entries */
189 			if(entry_num==part_num) { /* part found */
190 				goto found;
191 			}
192 			entry_num++; /* count partitions Entries (boot and non bootables */
193 			offset+=0x20;
194 			continue;
195 		}
196 		if ((pide->boot_ind==0x90) ||	/* Section Header Entry */
197 		    (pide->boot_ind==0x91) ||	/* Section Header Entry (last) */
198 		    (pide->boot_ind==0x44)) {	/* Extension Indicator */
199 			offset+=0x20; /* skip unused entries */
200 		}
201 		else {
202 			if(verb)
203 				printf ("** Partition %d not found on device %d **\n",
204 					part_num,dev_desc->dev);
205 			return(-1);
206 		}
207 	}
208 	/* if we reach this point entire sector has been
209 	 * searched w/o succsess */
210 	if(verb)
211 		printf ("** Partition %d not found on device %d **\n",
212 			part_num,dev_desc->dev);
213 	return(-1);
214 found:
215 	if(pide->boot_ind!=0x88) {
216 		if(verb)
217 			printf ("** Partition %d is not bootable on device %d **\n",
218 				part_num,dev_desc->dev);
219 		return (-1);
220 	}
221 	switch(pide->boot_media) {
222 		case 0x00: /* no emulation */
223 			info->size=le16_to_int(pide->sec_cnt)>>2;
224 			break;
225 		case 0x01:	info->size=2400>>2; break; /* 1.2MByte Floppy */
226 		case 0x02:	info->size=2880>>2; break; /* 1.44MByte Floppy */
227 		case 0x03:	info->size=5760>>2; break; /* 2.88MByte Floppy */
228 		case 0x04:	info->size=2880>>2; break; /* dummy (HD Emulation) */
229 		default:	info->size=0; break;
230 	}
231 	newblkaddr=le32_to_int(pide->rel_block_addr);
232 	info->start=newblkaddr;
233 	PRINTF(" part %d found @ %lx size %lx\n",part_num,newblkaddr,info->size);
234 	return 0;
235 }
236 
237 int get_partition_info_iso(block_dev_desc_t * dev_desc, int part_num, disk_partition_t * info)
238 {
239 	return(get_partition_info_iso_verb(dev_desc, part_num, info, 1));
240 }
241 
242 
243 void print_part_iso(block_dev_desc_t * dev_desc)
244 {
245 	disk_partition_t info;
246 	int i;
247 	if(get_partition_info_iso_verb(dev_desc,0,&info,0)==-1) {
248 		printf("** No boot partition found on device %d **\n",dev_desc->dev);
249 		return;
250 	}
251 	printf("Part   Start     Sect x Size Type\n");
252 	i=0;
253 	do {
254 		printf (" %2d %8ld %8ld %6ld %.32s\n",
255 			i, info.start, info.size, info.blksz, info.type);
256 		i++;
257 	} while (get_partition_info_iso_verb(dev_desc,i,&info,0)!=-1);
258 }
259 
260 int test_part_iso (block_dev_desc_t *dev_desc)
261 {
262 	disk_partition_t info;
263 
264 	return(get_partition_info_iso_verb(dev_desc,0,&info,0));
265 }
266 
267 #endif
268