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 | |
wdenk | 1968e61 | 2005-02-24 23:23:29 +0000 | [diff] [blame] | 15 | /* #define ISO_PART_DEBUG */ |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 16 | |
| 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 | |
Stefan Agner | 5c27535 | 2017-08-23 09:46:17 -0700 | [diff] [blame] | 27 | static unsigned char tmpbuf[CD_SECTSIZE] __aligned(ARCH_DMA_MINALIGN); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 28 | |
Alexander Graf | a2adb17 | 2016-04-11 16:16:16 +0200 | [diff] [blame] | 29 | unsigned 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 | |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 48 | /* only boot records will be listed as valid partitions */ |
Simon Glass | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 49 | int part_get_info_iso_verb(struct blk_desc *desc, int part_num, |
Simon Glass | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 50 | struct disk_partition *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 | |
Simon Glass | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 61 | if (desc->blksz != CD_SECTSIZE && desc->blksz != 512) |
Egbert Eich | d7ea4d4 | 2013-04-09 21:11:54 +0000 | [diff] [blame] | 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 | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 66 | if (iso_dread(desc, PVD_OFFSET, 1, (ulong *)tmpbuf) != 1) |
Stephen Warren | 7c4213f | 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 | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 71 | desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 72 | return (-1); |
| 73 | } |
Wolfgang Denk | 77ddac9 | 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 | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 77 | ppr->stand_ident, desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 78 | return (-1); |
| 79 | } |
Alexander Graf | ef9e6de | 2016-04-11 16:16:14 +0200 | [diff] [blame] | 80 | 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); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 83 | PRINTF(" Lastsect:%08lx\n",lastsect); |
| 84 | for(i=blkaddr;i<lastsect;i++) { |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 85 | PRINTF("Reading block %d\n", i); |
Simon Glass | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 86 | if (iso_dread(desc, i, 1, (ulong *)tmpbuf) != 1) |
Stephen Warren | 7c4213f | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 87 | return -1; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 88 | 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 Glass | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 93 | desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 94 | return (-1); |
| 95 | } |
| 96 | } |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 97 | /* boot entry found */ |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 98 | 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 Glass | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 101 | pbr->ident_str, desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 102 | return (-1); |
| 103 | } |
Simon Glass | 0919228 | 2016-03-16 07:45:34 -0600 | [diff] [blame] | 104 | bootaddr = get_unaligned_le32(pbr->pointer); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 105 | PRINTF(" Boot Entry at: %08lX\n",bootaddr); |
Simon Glass | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 106 | if (iso_dread(desc, bootaddr, 1, (ulong *)tmpbuf) != 1) { |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 107 | if(verb) |
| 108 | printf ("** Can't read Boot Entry at %lX on %d:%d **\n", |
Simon Glass | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 109 | bootaddr, desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 110 | return (-1); |
| 111 | } |
| 112 | chksum=0; |
| 113 | chksumbuf = (unsigned short *)tmpbuf; |
| 114 | for(i=0;i<0x10;i++) |
Alexander Graf | ef9e6de | 2016-04-11 16:16:14 +0200 | [diff] [blame] | 115 | chksum += le16_to_cpu(chksumbuf[i]); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 116 | if(chksum!=0) { |
| 117 | if(verb) |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 118 | printf("** Checksum Error in booting catalog validation entry on %d:%d **\n", |
Simon Glass | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 119 | desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 120 | 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 Glass | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 125 | desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 126 | 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 Glass | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 132 | desc->devnum, part_num); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 133 | return(-1); |
| 134 | } |
| 135 | #endif |
| 136 | /* the validation entry seems to be ok, now search the "partition" */ |
Alexander Graf | 2579c67 | 2016-04-11 16:16:15 +0200 | [diff] [blame] | 137 | entry_num=1; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 138 | offset=0x20; |
Ben Whitten | 192bc69 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 139 | strcpy((char *)info->type, "U-Boot"); |
Simon Glass | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 140 | part_set_generic_name(desc, part_num, (char *)info->name); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 141 | /* the bootcatalog (including validation Entry) is limited to 2048Bytes |
| 142 | * (63 boot entries + validation entry) */ |
| 143 | while(offset<2048) { |
| 144 | pide=(iso_init_def_entry_t *)&tmpbuf[offset]; |
| 145 | if ((pide->boot_ind==0x88) || |
| 146 | (pide->boot_ind==0x00)) { /* Header Id for default Sections Entries */ |
| 147 | if(entry_num==part_num) { /* part found */ |
| 148 | goto found; |
| 149 | } |
| 150 | entry_num++; /* count partitions Entries (boot and non bootables */ |
| 151 | offset+=0x20; |
| 152 | continue; |
| 153 | } |
| 154 | if ((pide->boot_ind==0x90) || /* Section Header Entry */ |
| 155 | (pide->boot_ind==0x91) || /* Section Header Entry (last) */ |
| 156 | (pide->boot_ind==0x44)) { /* Extension Indicator */ |
| 157 | offset+=0x20; /* skip unused entries */ |
| 158 | } |
| 159 | else { |
| 160 | if(verb) |
| 161 | printf ("** Partition %d not found on device %d **\n", |
Simon Glass | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 162 | part_num, desc->devnum); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 163 | return(-1); |
| 164 | } |
| 165 | } |
| 166 | /* if we reach this point entire sector has been |
| 167 | * searched w/o succsess */ |
| 168 | if(verb) |
| 169 | printf ("** Partition %d not found on device %d **\n", |
Simon Glass | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 170 | part_num, desc->devnum); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 171 | return(-1); |
| 172 | found: |
| 173 | if(pide->boot_ind!=0x88) { |
| 174 | if(verb) |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 175 | printf("** Partition %d is not bootable on device %d **\n", |
Simon Glass | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 176 | part_num, desc->devnum); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 177 | return (-1); |
| 178 | } |
| 179 | switch(pide->boot_media) { |
| 180 | case 0x00: /* no emulation */ |
Simon Glass | 0919228 | 2016-03-16 07:45:34 -0600 | [diff] [blame] | 181 | info->size = get_unaligned_le16(pide->sec_cnt)>>2; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 182 | break; |
| 183 | case 0x01: info->size=2400>>2; break; /* 1.2MByte Floppy */ |
| 184 | case 0x02: info->size=2880>>2; break; /* 1.44MByte Floppy */ |
| 185 | case 0x03: info->size=5760>>2; break; /* 2.88MByte Floppy */ |
| 186 | case 0x04: info->size=2880>>2; break; /* dummy (HD Emulation) */ |
| 187 | default: info->size=0; break; |
| 188 | } |
Simon Glass | 0919228 | 2016-03-16 07:45:34 -0600 | [diff] [blame] | 189 | newblkaddr = get_unaligned_le32(pide->rel_block_addr); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 190 | info->start=newblkaddr; |
Alexander Graf | a2adb17 | 2016-04-11 16:16:16 +0200 | [diff] [blame] | 191 | |
Simon Glass | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 192 | if (desc->blksz == 512) { |
Alexander Graf | a2adb17 | 2016-04-11 16:16:16 +0200 | [diff] [blame] | 193 | info->size *= 4; |
| 194 | info->start *= 4; |
| 195 | info->blksz = 512; |
| 196 | } |
| 197 | |
| 198 | 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] | 199 | return 0; |
| 200 | } |
| 201 | |
Simon Glass | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 202 | static int part_get_info_iso(struct blk_desc *desc, int part_num, |
Simon Glass | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 203 | struct disk_partition *info) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 204 | { |
Simon Glass | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 205 | return part_get_info_iso_verb(desc, part_num, info, 0); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Simon Glass | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 208 | static void part_print_iso(struct blk_desc *desc) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 209 | { |
Simon Glass | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 210 | struct disk_partition info; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 211 | int i; |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 212 | |
Simon Glass | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 213 | if (part_get_info_iso_verb(desc, 1, &info, 0) == -1) { |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 214 | printf("** No boot partition found on device %d **\n", |
Simon Glass | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 215 | desc->devnum); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 216 | return; |
| 217 | } |
| 218 | printf("Part Start Sect x Size Type\n"); |
Alexander Graf | 28f0014 | 2016-07-21 01:31:56 +0200 | [diff] [blame] | 219 | i=1; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 220 | do { |
Heinrich Schuchardt | d9d8849 | 2021-08-06 00:51:46 +0200 | [diff] [blame] | 221 | printf(" %2d %8" LBAFlength "u %8" LBAFlength "u %6ld %.32s\n", |
Frederic Leroy | 04735e9 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 222 | i, info.start, info.size, info.blksz, info.type); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 223 | i++; |
Simon Glass | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 224 | } while (part_get_info_iso_verb(desc, i, &info, 0) != -1); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Simon Glass | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 227 | static int part_test_iso(struct blk_desc *desc) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 228 | { |
Simon Glass | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 229 | struct disk_partition info; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 230 | |
Simon Glass | 06e293e | 2023-08-24 13:55:28 -0600 | [diff] [blame] | 231 | return part_get_info_iso_verb(desc, 1, &info, 0); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Simon Glass | 96e5b03 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 234 | U_BOOT_PART_TYPE(iso) = { |
| 235 | .name = "ISO", |
| 236 | .part_type = PART_TYPE_ISO, |
Petr Kulhavy | 87b8530 | 2016-09-09 10:27:15 +0200 | [diff] [blame] | 237 | .max_entries = ISO_ENTRY_NUMBERS, |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 238 | .get_info = part_get_info_iso, |
Simon Glass | 084bf4c | 2016-02-29 15:26:04 -0700 | [diff] [blame] | 239 | .print = part_print_iso, |
| 240 | .test = part_test_iso, |
Simon Glass | 96e5b03 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 241 | }; |