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