Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2001 |
| 4 | * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch. |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <command.h> |
Simon Glass | 0919228 | 2016-03-16 07:45:34 -0600 | [diff] [blame] | 9 | #include <asm/unaligned.h> |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 10 | #include "part_iso.h" |
| 11 | |
Adam Ford | 1811a92 | 2018-02-06 12:43:56 -0600 | [diff] [blame] | 12 | #ifdef CONFIG_HAVE_BLOCK_DEVICE |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 13 | |
wdenk | 1968e61 | 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 | |
Stefan Agner | 5c27535 | 2017-08-23 09:46:17 -0700 | [diff] [blame] | 26 | static unsigned char tmpbuf[CD_SECTSIZE] __aligned(ARCH_DMA_MINALIGN); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 27 | |
Alexander Graf | a2adb17 | 2016-04-11 16:16:16 +0200 | [diff] [blame] | 28 | unsigned long iso_dread(struct blk_desc *block_dev, lbaint_t start, |
| 29 | lbaint_t blkcnt, void *buffer) |
| 30 | { |
| 31 | unsigned long ret; |
| 32 | |
| 33 | if (block_dev->blksz == 512) { |
| 34 | /* Convert from 2048 to 512 sector size */ |
| 35 | start *= 4; |
| 36 | blkcnt *= 4; |
| 37 | } |
| 38 | |
| 39 | ret = blk_dread(block_dev, start, blkcnt, buffer); |
| 40 | |
| 41 | if (block_dev->blksz == 512) |
| 42 | ret /= 4; |
| 43 | |
| 44 | return ret; |
| 45 | } |
| 46 | |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 47 | /* only boot records will be listed as valid partitions */ |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 48 | int part_get_info_iso_verb(struct blk_desc *dev_desc, int part_num, |
| 49 | disk_partition_t *info, int verb) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 50 | { |
| 51 | int i,offset,entry_num; |
| 52 | unsigned short *chksumbuf; |
| 53 | unsigned short chksum; |
| 54 | unsigned long newblkaddr,blkaddr,lastsect,bootaddr; |
| 55 | iso_boot_rec_t *pbr = (iso_boot_rec_t *)tmpbuf; /* boot record */ |
| 56 | iso_pri_rec_t *ppr = (iso_pri_rec_t *)tmpbuf; /* primary desc */ |
| 57 | iso_val_entry_t *pve = (iso_val_entry_t *)tmpbuf; |
| 58 | iso_init_def_entry_t *pide; |
| 59 | |
Alexander Graf | a2adb17 | 2016-04-11 16:16:16 +0200 | [diff] [blame] | 60 | if ((dev_desc->blksz != CD_SECTSIZE) && (dev_desc->blksz != 512)) |
Egbert Eich | d7ea4d4 | 2013-04-09 21:11:54 +0000 | [diff] [blame] | 61 | return -1; |
| 62 | |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 63 | /* the first sector (sector 0x10) must be a primary volume desc */ |
| 64 | blkaddr=PVD_OFFSET; |
Alexander Graf | a2adb17 | 2016-04-11 16:16:16 +0200 | [diff] [blame] | 65 | if (iso_dread(dev_desc, PVD_OFFSET, 1, (ulong *)tmpbuf) != 1) |
Stephen Warren | 7c4213f | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 66 | return -1; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 67 | if(ppr->desctype!=0x01) { |
| 68 | if(verb) |
| 69 | printf ("** First descriptor is NOT a primary desc on %d:%d **\n", |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 70 | dev_desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 71 | return (-1); |
| 72 | } |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 73 | if(strncmp((char *)ppr->stand_ident,"CD001",5)!=0) { |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 74 | if(verb) |
| 75 | printf ("** Wrong ISO Ident: %s on %d:%d **\n", |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 76 | ppr->stand_ident, dev_desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 77 | return (-1); |
| 78 | } |
Alexander Graf | ef9e6de | 2016-04-11 16:16:14 +0200 | [diff] [blame] | 79 | lastsect = le32_to_cpu(ppr->firstsek_LEpathtab1_LE); |
| 80 | /* assuming same block size for all entries */ |
| 81 | info->blksz = be16_to_cpu(ppr->secsize_BE); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 82 | PRINTF(" Lastsect:%08lx\n",lastsect); |
| 83 | for(i=blkaddr;i<lastsect;i++) { |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 84 | PRINTF("Reading block %d\n", i); |
Alexander Graf | a2adb17 | 2016-04-11 16:16:16 +0200 | [diff] [blame] | 85 | if (iso_dread(dev_desc, i, 1, (ulong *)tmpbuf) != 1) |
Stephen Warren | 7c4213f | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 86 | return -1; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 87 | if(ppr->desctype==0x00) |
| 88 | break; /* boot entry found */ |
| 89 | if(ppr->desctype==0xff) { |
| 90 | if(verb) |
| 91 | printf ("** No valid boot catalog found on %d:%d **\n", |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 92 | dev_desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 93 | return (-1); |
| 94 | } |
| 95 | } |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 96 | /* boot entry found */ |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 97 | if(strncmp(pbr->ident_str,"EL TORITO SPECIFICATION",23)!=0) { |
| 98 | if(verb) |
| 99 | printf ("** Wrong El Torito ident: %s on %d:%d **\n", |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 100 | pbr->ident_str, dev_desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 101 | return (-1); |
| 102 | } |
Simon Glass | 0919228 | 2016-03-16 07:45:34 -0600 | [diff] [blame] | 103 | bootaddr = get_unaligned_le32(pbr->pointer); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 104 | PRINTF(" Boot Entry at: %08lX\n",bootaddr); |
Alexander Graf | a2adb17 | 2016-04-11 16:16:16 +0200 | [diff] [blame] | 105 | if (iso_dread(dev_desc, bootaddr, 1, (ulong *)tmpbuf) != 1) { |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 106 | if(verb) |
| 107 | printf ("** Can't read Boot Entry at %lX on %d:%d **\n", |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 108 | bootaddr, dev_desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 109 | return (-1); |
| 110 | } |
| 111 | chksum=0; |
| 112 | chksumbuf = (unsigned short *)tmpbuf; |
| 113 | for(i=0;i<0x10;i++) |
Alexander Graf | ef9e6de | 2016-04-11 16:16:14 +0200 | [diff] [blame] | 114 | chksum += le16_to_cpu(chksumbuf[i]); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 115 | if(chksum!=0) { |
| 116 | if(verb) |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 117 | printf("** Checksum Error in booting catalog validation entry on %d:%d **\n", |
| 118 | dev_desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 119 | return (-1); |
| 120 | } |
| 121 | if((pve->key[0]!=0x55)||(pve->key[1]!=0xAA)) { |
| 122 | if(verb) |
| 123 | printf ("** Key 0x55 0xAA error on %d:%d **\n", |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 124 | dev_desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 125 | return(-1); |
| 126 | } |
| 127 | #ifdef CHECK_FOR_POWERPC_PLATTFORM |
| 128 | if(pve->platform!=0x01) { |
| 129 | if(verb) |
| 130 | printf ("** No PowerPC platform CD on %d:%d **\n", |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 131 | dev_desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 132 | return(-1); |
| 133 | } |
| 134 | #endif |
| 135 | /* the validation entry seems to be ok, now search the "partition" */ |
Alexander Graf | 2579c67 | 2016-04-11 16:16:15 +0200 | [diff] [blame] | 136 | entry_num=1; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 137 | offset=0x20; |
Ben Whitten | 192bc69 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 138 | strcpy((char *)info->type, "U-Boot"); |
Petr Kulhavy | da2ee24 | 2016-09-09 10:27:17 +0200 | [diff] [blame] | 139 | part_set_generic_name(dev_desc, part_num, (char *)info->name); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 140 | /* the bootcatalog (including validation Entry) is limited to 2048Bytes |
| 141 | * (63 boot entries + validation entry) */ |
| 142 | while(offset<2048) { |
| 143 | pide=(iso_init_def_entry_t *)&tmpbuf[offset]; |
| 144 | if ((pide->boot_ind==0x88) || |
| 145 | (pide->boot_ind==0x00)) { /* Header Id for default Sections Entries */ |
| 146 | if(entry_num==part_num) { /* part found */ |
| 147 | goto found; |
| 148 | } |
| 149 | entry_num++; /* count partitions Entries (boot and non bootables */ |
| 150 | offset+=0x20; |
| 151 | continue; |
| 152 | } |
| 153 | if ((pide->boot_ind==0x90) || /* Section Header Entry */ |
| 154 | (pide->boot_ind==0x91) || /* Section Header Entry (last) */ |
| 155 | (pide->boot_ind==0x44)) { /* Extension Indicator */ |
| 156 | offset+=0x20; /* skip unused entries */ |
| 157 | } |
| 158 | else { |
| 159 | if(verb) |
| 160 | printf ("** Partition %d not found on device %d **\n", |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 161 | part_num, dev_desc->devnum); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 162 | return(-1); |
| 163 | } |
| 164 | } |
| 165 | /* if we reach this point entire sector has been |
| 166 | * searched w/o succsess */ |
| 167 | if(verb) |
| 168 | printf ("** Partition %d not found on device %d **\n", |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 169 | part_num, dev_desc->devnum); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 170 | return(-1); |
| 171 | found: |
| 172 | if(pide->boot_ind!=0x88) { |
| 173 | if(verb) |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 174 | printf("** Partition %d is not bootable on device %d **\n", |
| 175 | part_num, dev_desc->devnum); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 176 | return (-1); |
| 177 | } |
| 178 | switch(pide->boot_media) { |
| 179 | case 0x00: /* no emulation */ |
Simon Glass | 0919228 | 2016-03-16 07:45:34 -0600 | [diff] [blame] | 180 | info->size = get_unaligned_le16(pide->sec_cnt)>>2; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 181 | break; |
| 182 | case 0x01: info->size=2400>>2; break; /* 1.2MByte Floppy */ |
| 183 | case 0x02: info->size=2880>>2; break; /* 1.44MByte Floppy */ |
| 184 | case 0x03: info->size=5760>>2; break; /* 2.88MByte Floppy */ |
| 185 | case 0x04: info->size=2880>>2; break; /* dummy (HD Emulation) */ |
| 186 | default: info->size=0; break; |
| 187 | } |
Simon Glass | 0919228 | 2016-03-16 07:45:34 -0600 | [diff] [blame] | 188 | newblkaddr = get_unaligned_le32(pide->rel_block_addr); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 189 | info->start=newblkaddr; |
Alexander Graf | a2adb17 | 2016-04-11 16:16:16 +0200 | [diff] [blame] | 190 | |
| 191 | if (dev_desc->blksz == 512) { |
| 192 | info->size *= 4; |
| 193 | info->start *= 4; |
| 194 | info->blksz = 512; |
| 195 | } |
| 196 | |
| 197 | PRINTF(" part %d found @ %lx size %lx\n",part_num,info->start,info->size); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 201 | static int part_get_info_iso(struct blk_desc *dev_desc, int part_num, |
Simon Glass | 96e5b03 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 202 | disk_partition_t *info) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 203 | { |
Alexander Graf | b16339e | 2017-10-06 13:35:07 +0200 | [diff] [blame] | 204 | return part_get_info_iso_verb(dev_desc, part_num, info, 0); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Simon Glass | 084bf4c | 2016-02-29 15:26:04 -0700 | [diff] [blame] | 207 | static void part_print_iso(struct blk_desc *dev_desc) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 208 | { |
| 209 | disk_partition_t info; |
| 210 | int i; |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 211 | |
Alexander Graf | 28f0014 | 2016-07-21 01:31:56 +0200 | [diff] [blame] | 212 | if (part_get_info_iso_verb(dev_desc, 1, &info, 0) == -1) { |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 213 | printf("** No boot partition found on device %d **\n", |
| 214 | dev_desc->devnum); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 215 | return; |
| 216 | } |
| 217 | printf("Part Start Sect x Size Type\n"); |
Alexander Graf | 28f0014 | 2016-07-21 01:31:56 +0200 | [diff] [blame] | 218 | i=1; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 219 | do { |
Frederic Leroy | 04735e9 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 220 | printf(" %2d " LBAFU " " LBAFU " %6ld %.32s\n", |
| 221 | i, info.start, info.size, info.blksz, info.type); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 222 | i++; |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 223 | } while (part_get_info_iso_verb(dev_desc, i, &info, 0) != -1); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Simon Glass | 084bf4c | 2016-02-29 15:26:04 -0700 | [diff] [blame] | 226 | static int part_test_iso(struct blk_desc *dev_desc) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 227 | { |
| 228 | disk_partition_t info; |
| 229 | |
Alexander Graf | b16339e | 2017-10-06 13:35:07 +0200 | [diff] [blame] | 230 | return part_get_info_iso_verb(dev_desc, 1, &info, 0); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Simon Glass | 96e5b03 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 233 | U_BOOT_PART_TYPE(iso) = { |
| 234 | .name = "ISO", |
| 235 | .part_type = PART_TYPE_ISO, |
Petr Kulhavy | 87b8530 | 2016-09-09 10:27:15 +0200 | [diff] [blame] | 236 | .max_entries = ISO_ENTRY_NUMBERS, |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 237 | .get_info = part_get_info_iso, |
Simon Glass | 084bf4c | 2016-02-29 15:26:04 -0700 | [diff] [blame] | 238 | .print = part_print_iso, |
| 239 | .test = part_test_iso, |
Simon Glass | 96e5b03 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 240 | }; |
Jon Loeliger | cde5c64 | 2007-07-09 17:22:37 -0500 | [diff] [blame] | 241 | #endif |