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