blob: 9f5c50c73faba4cf2c51bd030f70401914a78092 [file] [log] [blame]
wdenkcc1c8a12002-11-02 22:58:18 +00001/*
2 * (C) Copyright 2001
3 * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch.
4 *
Wolfgang Denk3765b3e2013-10-07 13:07:26 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkcc1c8a12002-11-02 22:58:18 +00006 */
7
8#include <common.h>
9#include <command.h>
Simon Glass09192282016-03-16 07:45:34 -060010#include <asm/unaligned.h>
wdenkcc1c8a12002-11-02 22:58:18 +000011#include "part_iso.h"
12
Stephen Warren2c1af9d2013-02-28 15:03:46 +000013#ifdef HAVE_BLOCK_DEVICE
wdenkcc1c8a12002-11-02 22:58:18 +000014
wdenk1968e612005-02-24 23:23:29 +000015/* #define ISO_PART_DEBUG */
wdenkcc1c8a12002-11-02 22:58:18 +000016
17#ifdef ISO_PART_DEBUG
18#define PRINTF(fmt,args...) printf (fmt ,##args)
19#else
20#define PRINTF(fmt,args...)
21#endif
22
23/* enable this if CDs are written with the PowerPC Platform ID */
24#undef CHECK_FOR_POWERPC_PLATTFORM
25#define CD_SECTSIZE 2048
26
27static unsigned char tmpbuf[CD_SECTSIZE];
28
Alexander Grafa2adb172016-04-11 16:16:16 +020029unsigned long iso_dread(struct blk_desc *block_dev, lbaint_t start,
30 lbaint_t blkcnt, void *buffer)
31{
32 unsigned long ret;
33
34 if (block_dev->blksz == 512) {
35 /* Convert from 2048 to 512 sector size */
36 start *= 4;
37 blkcnt *= 4;
38 }
39
40 ret = blk_dread(block_dev, start, blkcnt, buffer);
41
42 if (block_dev->blksz == 512)
43 ret /= 4;
44
45 return ret;
46}
47
wdenkcc1c8a12002-11-02 22:58:18 +000048/* only boot records will be listed as valid partitions */
Simon Glass3e8bd462016-02-29 15:25:48 -070049int part_get_info_iso_verb(struct blk_desc *dev_desc, int part_num,
50 disk_partition_t *info, int verb)
wdenkcc1c8a12002-11-02 22:58:18 +000051{
52 int i,offset,entry_num;
53 unsigned short *chksumbuf;
54 unsigned short chksum;
55 unsigned long newblkaddr,blkaddr,lastsect,bootaddr;
56 iso_boot_rec_t *pbr = (iso_boot_rec_t *)tmpbuf; /* boot record */
57 iso_pri_rec_t *ppr = (iso_pri_rec_t *)tmpbuf; /* primary desc */
58 iso_val_entry_t *pve = (iso_val_entry_t *)tmpbuf;
59 iso_init_def_entry_t *pide;
60
Alexander Grafa2adb172016-04-11 16:16:16 +020061 if ((dev_desc->blksz != CD_SECTSIZE) && (dev_desc->blksz != 512))
Egbert Eichd7ea4d42013-04-09 21:11:54 +000062 return -1;
63
wdenkcc1c8a12002-11-02 22:58:18 +000064 /* the first sector (sector 0x10) must be a primary volume desc */
65 blkaddr=PVD_OFFSET;
Alexander Grafa2adb172016-04-11 16:16:16 +020066 if (iso_dread(dev_desc, PVD_OFFSET, 1, (ulong *)tmpbuf) != 1)
Stephen Warren7c4213f2015-12-07 11:38:48 -070067 return -1;
wdenkcc1c8a12002-11-02 22:58:18 +000068 if(ppr->desctype!=0x01) {
69 if(verb)
70 printf ("** First descriptor is NOT a primary desc on %d:%d **\n",
Simon Glassbcce53d2016-02-29 15:25:51 -070071 dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +000072 return (-1);
73 }
Wolfgang Denk77ddac92005-10-13 16:45:02 +020074 if(strncmp((char *)ppr->stand_ident,"CD001",5)!=0) {
wdenkcc1c8a12002-11-02 22:58:18 +000075 if(verb)
76 printf ("** Wrong ISO Ident: %s on %d:%d **\n",
Simon Glassbcce53d2016-02-29 15:25:51 -070077 ppr->stand_ident, dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +000078 return (-1);
79 }
Alexander Grafef9e6de2016-04-11 16:16:14 +020080 lastsect = le32_to_cpu(ppr->firstsek_LEpathtab1_LE);
81 /* assuming same block size for all entries */
82 info->blksz = be16_to_cpu(ppr->secsize_BE);
wdenkcc1c8a12002-11-02 22:58:18 +000083 PRINTF(" Lastsect:%08lx\n",lastsect);
84 for(i=blkaddr;i<lastsect;i++) {
wdenkc7de8292002-11-19 11:04:11 +000085 PRINTF("Reading block %d\n", i);
Alexander Grafa2adb172016-04-11 16:16:16 +020086 if (iso_dread(dev_desc, i, 1, (ulong *)tmpbuf) != 1)
Stephen Warren7c4213f2015-12-07 11:38:48 -070087 return -1;
wdenkcc1c8a12002-11-02 22:58:18 +000088 if(ppr->desctype==0x00)
89 break; /* boot entry found */
90 if(ppr->desctype==0xff) {
91 if(verb)
92 printf ("** No valid boot catalog found on %d:%d **\n",
Simon Glassbcce53d2016-02-29 15:25:51 -070093 dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +000094 return (-1);
95 }
96 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +020097 /* boot entry found */
wdenkcc1c8a12002-11-02 22:58:18 +000098 if(strncmp(pbr->ident_str,"EL TORITO SPECIFICATION",23)!=0) {
99 if(verb)
100 printf ("** Wrong El Torito ident: %s on %d:%d **\n",
Simon Glassbcce53d2016-02-29 15:25:51 -0700101 pbr->ident_str, dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000102 return (-1);
103 }
Simon Glass09192282016-03-16 07:45:34 -0600104 bootaddr = get_unaligned_le32(pbr->pointer);
wdenkcc1c8a12002-11-02 22:58:18 +0000105 PRINTF(" Boot Entry at: %08lX\n",bootaddr);
Alexander Grafa2adb172016-04-11 16:16:16 +0200106 if (iso_dread(dev_desc, bootaddr, 1, (ulong *)tmpbuf) != 1) {
wdenkcc1c8a12002-11-02 22:58:18 +0000107 if(verb)
108 printf ("** Can't read Boot Entry at %lX on %d:%d **\n",
Simon Glassbcce53d2016-02-29 15:25:51 -0700109 bootaddr, dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000110 return (-1);
111 }
112 chksum=0;
113 chksumbuf = (unsigned short *)tmpbuf;
114 for(i=0;i<0x10;i++)
Alexander Grafef9e6de2016-04-11 16:16:14 +0200115 chksum += le16_to_cpu(chksumbuf[i]);
wdenkcc1c8a12002-11-02 22:58:18 +0000116 if(chksum!=0) {
117 if(verb)
Simon Glassbcce53d2016-02-29 15:25:51 -0700118 printf("** Checksum Error in booting catalog validation entry on %d:%d **\n",
119 dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000120 return (-1);
121 }
122 if((pve->key[0]!=0x55)||(pve->key[1]!=0xAA)) {
123 if(verb)
124 printf ("** Key 0x55 0xAA error on %d:%d **\n",
Simon Glassbcce53d2016-02-29 15:25:51 -0700125 dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000126 return(-1);
127 }
128#ifdef CHECK_FOR_POWERPC_PLATTFORM
129 if(pve->platform!=0x01) {
130 if(verb)
131 printf ("** No PowerPC platform CD on %d:%d **\n",
Simon Glassbcce53d2016-02-29 15:25:51 -0700132 dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000133 return(-1);
134 }
135#endif
136 /* the validation entry seems to be ok, now search the "partition" */
Alexander Graf2579c672016-04-11 16:16:15 +0200137 entry_num=1;
wdenkcc1c8a12002-11-02 22:58:18 +0000138 offset=0x20;
Ben Whitten192bc692015-12-30 13:05:58 +0000139 strcpy((char *)info->type, "U-Boot");
wdenkcc1c8a12002-11-02 22:58:18 +0000140 switch(dev_desc->if_type) {
141 case IF_TYPE_IDE:
Dave Liuc7057b52008-03-26 22:49:44 +0800142 case IF_TYPE_SATA:
wdenkcc1c8a12002-11-02 22:58:18 +0000143 case IF_TYPE_ATAPI:
Wolfgang Denk71665522009-07-28 22:35:39 +0200144 sprintf ((char *)info->name, "hd%c%d",
Simon Glassbcce53d2016-02-29 15:25:51 -0700145 'a' + dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000146 break;
147 case IF_TYPE_SCSI:
Wolfgang Denk71665522009-07-28 22:35:39 +0200148 sprintf ((char *)info->name, "sd%c%d",
Simon Glassbcce53d2016-02-29 15:25:51 -0700149 'a' + dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000150 break;
151 case IF_TYPE_USB:
Wolfgang Denk71665522009-07-28 22:35:39 +0200152 sprintf ((char *)info->name, "usbd%c%d",
Simon Glassbcce53d2016-02-29 15:25:51 -0700153 'a' + dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000154 break;
155 case IF_TYPE_DOC:
Wolfgang Denk71665522009-07-28 22:35:39 +0200156 sprintf ((char *)info->name, "docd%c%d",
Simon Glassbcce53d2016-02-29 15:25:51 -0700157 'a' + dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000158 break;
159 default:
Wolfgang Denk71665522009-07-28 22:35:39 +0200160 sprintf ((char *)info->name, "xx%c%d",
Simon Glassbcce53d2016-02-29 15:25:51 -0700161 'a' + dev_desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000162 break;
163 }
164 /* the bootcatalog (including validation Entry) is limited to 2048Bytes
165 * (63 boot entries + validation entry) */
166 while(offset<2048) {
167 pide=(iso_init_def_entry_t *)&tmpbuf[offset];
168 if ((pide->boot_ind==0x88) ||
169 (pide->boot_ind==0x00)) { /* Header Id for default Sections Entries */
170 if(entry_num==part_num) { /* part found */
171 goto found;
172 }
173 entry_num++; /* count partitions Entries (boot and non bootables */
174 offset+=0x20;
175 continue;
176 }
177 if ((pide->boot_ind==0x90) || /* Section Header Entry */
178 (pide->boot_ind==0x91) || /* Section Header Entry (last) */
179 (pide->boot_ind==0x44)) { /* Extension Indicator */
180 offset+=0x20; /* skip unused entries */
181 }
182 else {
183 if(verb)
184 printf ("** Partition %d not found on device %d **\n",
Simon Glassbcce53d2016-02-29 15:25:51 -0700185 part_num, dev_desc->devnum);
wdenkcc1c8a12002-11-02 22:58:18 +0000186 return(-1);
187 }
188 }
189 /* if we reach this point entire sector has been
190 * searched w/o succsess */
191 if(verb)
192 printf ("** Partition %d not found on device %d **\n",
Simon Glassbcce53d2016-02-29 15:25:51 -0700193 part_num, dev_desc->devnum);
wdenkcc1c8a12002-11-02 22:58:18 +0000194 return(-1);
195found:
196 if(pide->boot_ind!=0x88) {
197 if(verb)
Simon Glassbcce53d2016-02-29 15:25:51 -0700198 printf("** Partition %d is not bootable on device %d **\n",
199 part_num, dev_desc->devnum);
wdenkcc1c8a12002-11-02 22:58:18 +0000200 return (-1);
201 }
202 switch(pide->boot_media) {
203 case 0x00: /* no emulation */
Simon Glass09192282016-03-16 07:45:34 -0600204 info->size = get_unaligned_le16(pide->sec_cnt)>>2;
wdenkcc1c8a12002-11-02 22:58:18 +0000205 break;
206 case 0x01: info->size=2400>>2; break; /* 1.2MByte Floppy */
207 case 0x02: info->size=2880>>2; break; /* 1.44MByte Floppy */
208 case 0x03: info->size=5760>>2; break; /* 2.88MByte Floppy */
209 case 0x04: info->size=2880>>2; break; /* dummy (HD Emulation) */
210 default: info->size=0; break;
211 }
Simon Glass09192282016-03-16 07:45:34 -0600212 newblkaddr = get_unaligned_le32(pide->rel_block_addr);
wdenkcc1c8a12002-11-02 22:58:18 +0000213 info->start=newblkaddr;
Alexander Grafa2adb172016-04-11 16:16:16 +0200214
215 if (dev_desc->blksz == 512) {
216 info->size *= 4;
217 info->start *= 4;
218 info->blksz = 512;
219 }
220
221 PRINTF(" part %d found @ %lx size %lx\n",part_num,info->start,info->size);
wdenkcc1c8a12002-11-02 22:58:18 +0000222 return 0;
223}
224
Simon Glass3e8bd462016-02-29 15:25:48 -0700225static int part_get_info_iso(struct blk_desc *dev_desc, int part_num,
Simon Glass96e5b032016-02-29 15:25:47 -0700226 disk_partition_t *info)
wdenkcc1c8a12002-11-02 22:58:18 +0000227{
Simon Glass3e8bd462016-02-29 15:25:48 -0700228 return part_get_info_iso_verb(dev_desc, part_num, info, 1);
wdenkcc1c8a12002-11-02 22:58:18 +0000229}
230
Simon Glass084bf4c2016-02-29 15:26:04 -0700231static void part_print_iso(struct blk_desc *dev_desc)
wdenkcc1c8a12002-11-02 22:58:18 +0000232{
233 disk_partition_t info;
234 int i;
Simon Glass3e8bd462016-02-29 15:25:48 -0700235
236 if (part_get_info_iso_verb(dev_desc, 0, &info, 0) == -1) {
Simon Glassbcce53d2016-02-29 15:25:51 -0700237 printf("** No boot partition found on device %d **\n",
238 dev_desc->devnum);
wdenkcc1c8a12002-11-02 22:58:18 +0000239 return;
240 }
241 printf("Part Start Sect x Size Type\n");
242 i=0;
243 do {
Frederic Leroy04735e92013-06-26 18:11:25 +0200244 printf(" %2d " LBAFU " " LBAFU " %6ld %.32s\n",
245 i, info.start, info.size, info.blksz, info.type);
wdenkcc1c8a12002-11-02 22:58:18 +0000246 i++;
Simon Glass3e8bd462016-02-29 15:25:48 -0700247 } while (part_get_info_iso_verb(dev_desc, i, &info, 0) != -1);
wdenkcc1c8a12002-11-02 22:58:18 +0000248}
249
Simon Glass084bf4c2016-02-29 15:26:04 -0700250static int part_test_iso(struct blk_desc *dev_desc)
wdenkcc1c8a12002-11-02 22:58:18 +0000251{
252 disk_partition_t info;
253
Alexander Graf2579c672016-04-11 16:16:15 +0200254 return part_get_info_iso_verb(dev_desc, 1, &info, 1);
wdenkcc1c8a12002-11-02 22:58:18 +0000255}
256
Simon Glass96e5b032016-02-29 15:25:47 -0700257U_BOOT_PART_TYPE(iso) = {
258 .name = "ISO",
259 .part_type = PART_TYPE_ISO,
Simon Glass3e8bd462016-02-29 15:25:48 -0700260 .get_info = part_get_info_iso,
Simon Glass084bf4c2016-02-29 15:26:04 -0700261 .print = part_print_iso,
262 .test = part_test_iso,
Simon Glass96e5b032016-02-29 15:25:47 -0700263};
Jon Loeligercde5c642007-07-09 17:22:37 -0500264#endif