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