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