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