wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch. |
| 4 | * |
Wolfgang Denk | bd8ec7e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <command.h> |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 10 | #include "part_iso.h" |
| 11 | |
Stephen Warren | 8ec730d | 2013-02-28 15:03:46 +0000 | [diff] [blame] | 12 | #ifdef HAVE_BLOCK_DEVICE |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 13 | |
wdenk | acd0536 | 2005-02-24 23:23:29 +0000 | [diff] [blame] | 14 | /* #define ISO_PART_DEBUG */ |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 15 | |
| 16 | #ifdef ISO_PART_DEBUG |
| 17 | #define PRINTF(fmt,args...) printf (fmt ,##args) |
| 18 | #else |
| 19 | #define PRINTF(fmt,args...) |
| 20 | #endif |
| 21 | |
| 22 | /* enable this if CDs are written with the PowerPC Platform ID */ |
| 23 | #undef CHECK_FOR_POWERPC_PLATTFORM |
| 24 | #define CD_SECTSIZE 2048 |
| 25 | |
| 26 | static unsigned char tmpbuf[CD_SECTSIZE]; |
| 27 | |
| 28 | /* Convert char[4] in little endian format to the host format integer |
| 29 | */ |
| 30 | static inline unsigned long le32_to_int(unsigned char *le32) |
| 31 | { |
| 32 | return ((le32[3] << 24) + |
| 33 | (le32[2] << 16) + |
| 34 | (le32[1] << 8) + |
| 35 | le32[0] |
| 36 | ); |
| 37 | } |
| 38 | /* Convert char[2] in little endian format to the host format integer |
| 39 | */ |
| 40 | static inline unsigned short le16_to_int(unsigned char *le16) |
| 41 | { |
| 42 | return ((le16[1] << 8) + |
| 43 | le16[0] |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | /* only boot records will be listed as valid partitions */ |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 49 | int part_get_info_iso_verb(struct blk_desc *dev_desc, int part_num, |
| 50 | disk_partition_t *info, int verb) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 51 | { |
| 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 | |
Egbert Eich | 9b2ecea | 2013-04-09 21:11:54 +0000 | [diff] [blame] | 61 | if (dev_desc->blksz != CD_SECTSIZE) |
| 62 | return -1; |
| 63 | |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 64 | /* the first sector (sector 0x10) must be a primary volume desc */ |
| 65 | blkaddr=PVD_OFFSET; |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 66 | if (blk_dread(dev_desc, PVD_OFFSET, 1, (ulong *)tmpbuf) != 1) |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 67 | return -1; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 68 | if(ppr->desctype!=0x01) { |
| 69 | if(verb) |
| 70 | printf ("** First descriptor is NOT a primary desc on %d:%d **\n", |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 71 | dev_desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 72 | return (-1); |
| 73 | } |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 74 | if(strncmp((char *)ppr->stand_ident,"CD001",5)!=0) { |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 75 | if(verb) |
| 76 | printf ("** Wrong ISO Ident: %s on %d:%d **\n", |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 77 | ppr->stand_ident, dev_desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 78 | return (-1); |
| 79 | } |
| 80 | lastsect= ((ppr->firstsek_LEpathtab1_LE & 0x000000ff)<<24) + |
| 81 | ((ppr->firstsek_LEpathtab1_LE & 0x0000ff00)<< 8) + |
| 82 | ((ppr->firstsek_LEpathtab1_LE & 0x00ff0000)>> 8) + |
| 83 | ((ppr->firstsek_LEpathtab1_LE & 0xff000000)>>24) ; |
| 84 | info->blksz=ppr->secsize_BE; /* assuming same block size for all entries */ |
| 85 | PRINTF(" Lastsect:%08lx\n",lastsect); |
| 86 | for(i=blkaddr;i<lastsect;i++) { |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 87 | PRINTF("Reading block %d\n", i); |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 88 | if (blk_dread(dev_desc, i, 1, (ulong *)tmpbuf) != 1) |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 89 | return -1; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 90 | if(ppr->desctype==0x00) |
| 91 | break; /* boot entry found */ |
| 92 | if(ppr->desctype==0xff) { |
| 93 | if(verb) |
| 94 | printf ("** No valid boot catalog found on %d:%d **\n", |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 95 | dev_desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 96 | return (-1); |
| 97 | } |
| 98 | } |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 99 | /* boot entry found */ |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 100 | if(strncmp(pbr->ident_str,"EL TORITO SPECIFICATION",23)!=0) { |
| 101 | if(verb) |
| 102 | printf ("** Wrong El Torito ident: %s on %d:%d **\n", |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 103 | pbr->ident_str, dev_desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 104 | return (-1); |
| 105 | } |
| 106 | bootaddr=le32_to_int(pbr->pointer); |
| 107 | PRINTF(" Boot Entry at: %08lX\n",bootaddr); |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 108 | if (blk_dread(dev_desc, bootaddr, 1, (ulong *)tmpbuf) != 1) { |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 109 | if(verb) |
| 110 | printf ("** Can't read Boot Entry at %lX on %d:%d **\n", |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 111 | bootaddr, dev_desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 112 | return (-1); |
| 113 | } |
| 114 | chksum=0; |
| 115 | chksumbuf = (unsigned short *)tmpbuf; |
| 116 | for(i=0;i<0x10;i++) |
| 117 | chksum+=((chksumbuf[i] &0xff)<<8)+((chksumbuf[i] &0xff00)>>8); |
| 118 | if(chksum!=0) { |
| 119 | if(verb) |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 120 | printf("** Checksum Error in booting catalog validation entry on %d:%d **\n", |
| 121 | dev_desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 122 | return (-1); |
| 123 | } |
| 124 | if((pve->key[0]!=0x55)||(pve->key[1]!=0xAA)) { |
| 125 | if(verb) |
| 126 | printf ("** Key 0x55 0xAA error on %d:%d **\n", |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 127 | dev_desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 128 | return(-1); |
| 129 | } |
| 130 | #ifdef CHECK_FOR_POWERPC_PLATTFORM |
| 131 | if(pve->platform!=0x01) { |
| 132 | if(verb) |
| 133 | printf ("** No PowerPC platform CD on %d:%d **\n", |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 134 | dev_desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 135 | return(-1); |
| 136 | } |
| 137 | #endif |
| 138 | /* the validation entry seems to be ok, now search the "partition" */ |
| 139 | entry_num=0; |
| 140 | offset=0x20; |
Ben Whitten | 34fd6c9 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 141 | strcpy((char *)info->type, "U-Boot"); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 142 | switch(dev_desc->if_type) { |
| 143 | case IF_TYPE_IDE: |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 144 | case IF_TYPE_SATA: |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 145 | case IF_TYPE_ATAPI: |
Wolfgang Denk | 03e5ce4 | 2009-07-28 22:35:39 +0200 | [diff] [blame] | 146 | sprintf ((char *)info->name, "hd%c%d", |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 147 | 'a' + dev_desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 148 | break; |
| 149 | case IF_TYPE_SCSI: |
Wolfgang Denk | 03e5ce4 | 2009-07-28 22:35:39 +0200 | [diff] [blame] | 150 | sprintf ((char *)info->name, "sd%c%d", |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 151 | 'a' + dev_desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 152 | break; |
| 153 | case IF_TYPE_USB: |
Wolfgang Denk | 03e5ce4 | 2009-07-28 22:35:39 +0200 | [diff] [blame] | 154 | sprintf ((char *)info->name, "usbd%c%d", |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 155 | 'a' + dev_desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 156 | break; |
| 157 | case IF_TYPE_DOC: |
Wolfgang Denk | 03e5ce4 | 2009-07-28 22:35:39 +0200 | [diff] [blame] | 158 | sprintf ((char *)info->name, "docd%c%d", |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 159 | 'a' + dev_desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 160 | break; |
| 161 | default: |
Wolfgang Denk | 03e5ce4 | 2009-07-28 22:35:39 +0200 | [diff] [blame] | 162 | sprintf ((char *)info->name, "xx%c%d", |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 163 | 'a' + dev_desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 164 | break; |
| 165 | } |
| 166 | /* the bootcatalog (including validation Entry) is limited to 2048Bytes |
| 167 | * (63 boot entries + validation entry) */ |
| 168 | while(offset<2048) { |
| 169 | pide=(iso_init_def_entry_t *)&tmpbuf[offset]; |
| 170 | if ((pide->boot_ind==0x88) || |
| 171 | (pide->boot_ind==0x00)) { /* Header Id for default Sections Entries */ |
| 172 | if(entry_num==part_num) { /* part found */ |
| 173 | goto found; |
| 174 | } |
| 175 | entry_num++; /* count partitions Entries (boot and non bootables */ |
| 176 | offset+=0x20; |
| 177 | continue; |
| 178 | } |
| 179 | if ((pide->boot_ind==0x90) || /* Section Header Entry */ |
| 180 | (pide->boot_ind==0x91) || /* Section Header Entry (last) */ |
| 181 | (pide->boot_ind==0x44)) { /* Extension Indicator */ |
| 182 | offset+=0x20; /* skip unused entries */ |
| 183 | } |
| 184 | else { |
| 185 | if(verb) |
| 186 | printf ("** Partition %d not found on device %d **\n", |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 187 | part_num, dev_desc->devnum); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 188 | return(-1); |
| 189 | } |
| 190 | } |
| 191 | /* if we reach this point entire sector has been |
| 192 | * searched w/o succsess */ |
| 193 | if(verb) |
| 194 | printf ("** Partition %d not found on device %d **\n", |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 195 | part_num, dev_desc->devnum); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 196 | return(-1); |
| 197 | found: |
| 198 | if(pide->boot_ind!=0x88) { |
| 199 | if(verb) |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 200 | printf("** Partition %d is not bootable on device %d **\n", |
| 201 | part_num, dev_desc->devnum); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 202 | return (-1); |
| 203 | } |
| 204 | switch(pide->boot_media) { |
| 205 | case 0x00: /* no emulation */ |
| 206 | info->size=le16_to_int(pide->sec_cnt)>>2; |
| 207 | break; |
| 208 | case 0x01: info->size=2400>>2; break; /* 1.2MByte Floppy */ |
| 209 | case 0x02: info->size=2880>>2; break; /* 1.44MByte Floppy */ |
| 210 | case 0x03: info->size=5760>>2; break; /* 2.88MByte Floppy */ |
| 211 | case 0x04: info->size=2880>>2; break; /* dummy (HD Emulation) */ |
| 212 | default: info->size=0; break; |
| 213 | } |
| 214 | newblkaddr=le32_to_int(pide->rel_block_addr); |
| 215 | info->start=newblkaddr; |
| 216 | PRINTF(" part %d found @ %lx size %lx\n",part_num,newblkaddr,info->size); |
| 217 | return 0; |
| 218 | } |
| 219 | |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 220 | static int part_get_info_iso(struct blk_desc *dev_desc, int part_num, |
Simon Glass | 83ce563 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 221 | disk_partition_t *info) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 222 | { |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 223 | return part_get_info_iso_verb(dev_desc, part_num, info, 1); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Simon Glass | 0f1c952 | 2016-02-29 15:26:04 -0700 | [diff] [blame] | 226 | static void part_print_iso(struct blk_desc *dev_desc) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 227 | { |
| 228 | disk_partition_t info; |
| 229 | int i; |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 230 | |
| 231 | if (part_get_info_iso_verb(dev_desc, 0, &info, 0) == -1) { |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 232 | printf("** No boot partition found on device %d **\n", |
| 233 | dev_desc->devnum); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 234 | return; |
| 235 | } |
| 236 | printf("Part Start Sect x Size Type\n"); |
| 237 | i=0; |
| 238 | do { |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 239 | printf(" %2d " LBAFU " " LBAFU " %6ld %.32s\n", |
| 240 | i, info.start, info.size, info.blksz, info.type); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 241 | i++; |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 242 | } while (part_get_info_iso_verb(dev_desc, i, &info, 0) != -1); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Simon Glass | 0f1c952 | 2016-02-29 15:26:04 -0700 | [diff] [blame] | 245 | static int part_test_iso(struct blk_desc *dev_desc) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 246 | { |
| 247 | disk_partition_t info; |
| 248 | |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 249 | return part_get_info_iso_verb(dev_desc, 0, &info, 0); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Simon Glass | 83ce563 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 252 | U_BOOT_PART_TYPE(iso) = { |
| 253 | .name = "ISO", |
| 254 | .part_type = PART_TYPE_ISO, |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 255 | .get_info = part_get_info_iso, |
Simon Glass | 0f1c952 | 2016-02-29 15:26:04 -0700 | [diff] [blame] | 256 | .print = part_print_iso, |
| 257 | .test = part_test_iso, |
Simon Glass | 83ce563 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 258 | }; |
Jon Loeliger | f7f54a8 | 2007-07-09 17:22:37 -0500 | [diff] [blame] | 259 | #endif |