blob: 6e05b2feffbab4073c309d56cc24dc14199b0846 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkcc1c8a12002-11-02 22:58:18 +00002/*
3 * (C) Copyright 2001
4 * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch.
wdenkcc1c8a12002-11-02 22:58:18 +00005 */
6
Simon Glasse6f6f9e2020-05-10 11:39:58 -06007#include <blk.h>
wdenkcc1c8a12002-11-02 22:58:18 +00008#include <command.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -06009#include <part.h>
Simon Glass90526e92020-05-10 11:39:56 -060010#include <asm/cache.h>
Simon Glass09192282016-03-16 07:45:34 -060011#include <asm/unaligned.h>
wdenkcc1c8a12002-11-02 22:58:18 +000012#include "part_iso.h"
13
wdenk1968e612005-02-24 23:23:29 +000014/* #define ISO_PART_DEBUG */
wdenkcc1c8a12002-11-02 22:58:18 +000015
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 Agner5c275352017-08-23 09:46:17 -070026static unsigned char tmpbuf[CD_SECTSIZE] __aligned(ARCH_DMA_MINALIGN);
wdenkcc1c8a12002-11-02 22:58:18 +000027
Alexander Grafa2adb172016-04-11 16:16:16 +020028unsigned 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
wdenkcc1c8a12002-11-02 22:58:18 +000047/* only boot records will be listed as valid partitions */
Simon Glass06e293e2023-08-24 13:55:28 -060048int part_get_info_iso_verb(struct blk_desc *desc, int part_num,
Simon Glass05289792020-05-10 11:39:57 -060049 struct disk_partition *info, int verb)
wdenkcc1c8a12002-11-02 22:58:18 +000050{
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
Simon Glass06e293e2023-08-24 13:55:28 -060060 if (desc->blksz != CD_SECTSIZE && desc->blksz != 512)
Egbert Eichd7ea4d42013-04-09 21:11:54 +000061 return -1;
62
wdenkcc1c8a12002-11-02 22:58:18 +000063 /* the first sector (sector 0x10) must be a primary volume desc */
64 blkaddr=PVD_OFFSET;
Simon Glass06e293e2023-08-24 13:55:28 -060065 if (iso_dread(desc, PVD_OFFSET, 1, (ulong *)tmpbuf) != 1)
Stephen Warren7c4213f2015-12-07 11:38:48 -070066 return -1;
wdenkcc1c8a12002-11-02 22:58:18 +000067 if(ppr->desctype!=0x01) {
68 if(verb)
69 printf ("** First descriptor is NOT a primary desc on %d:%d **\n",
Simon Glass06e293e2023-08-24 13:55:28 -060070 desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +000071 return (-1);
72 }
Wolfgang Denk77ddac92005-10-13 16:45:02 +020073 if(strncmp((char *)ppr->stand_ident,"CD001",5)!=0) {
wdenkcc1c8a12002-11-02 22:58:18 +000074 if(verb)
75 printf ("** Wrong ISO Ident: %s on %d:%d **\n",
Simon Glass06e293e2023-08-24 13:55:28 -060076 ppr->stand_ident, desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +000077 return (-1);
78 }
Alexander Grafef9e6de2016-04-11 16:16:14 +020079 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);
wdenkcc1c8a12002-11-02 22:58:18 +000082 PRINTF(" Lastsect:%08lx\n",lastsect);
83 for(i=blkaddr;i<lastsect;i++) {
wdenkc7de8292002-11-19 11:04:11 +000084 PRINTF("Reading block %d\n", i);
Simon Glass06e293e2023-08-24 13:55:28 -060085 if (iso_dread(desc, i, 1, (ulong *)tmpbuf) != 1)
Stephen Warren7c4213f2015-12-07 11:38:48 -070086 return -1;
wdenkcc1c8a12002-11-02 22:58:18 +000087 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 Glass06e293e2023-08-24 13:55:28 -060092 desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +000093 return (-1);
94 }
95 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +020096 /* boot entry found */
wdenkcc1c8a12002-11-02 22:58:18 +000097 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 Glass06e293e2023-08-24 13:55:28 -0600100 pbr->ident_str, desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000101 return (-1);
102 }
Simon Glass09192282016-03-16 07:45:34 -0600103 bootaddr = get_unaligned_le32(pbr->pointer);
wdenkcc1c8a12002-11-02 22:58:18 +0000104 PRINTF(" Boot Entry at: %08lX\n",bootaddr);
Simon Glass06e293e2023-08-24 13:55:28 -0600105 if (iso_dread(desc, bootaddr, 1, (ulong *)tmpbuf) != 1) {
wdenkcc1c8a12002-11-02 22:58:18 +0000106 if(verb)
107 printf ("** Can't read Boot Entry at %lX on %d:%d **\n",
Simon Glass06e293e2023-08-24 13:55:28 -0600108 bootaddr, desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000109 return (-1);
110 }
111 chksum=0;
112 chksumbuf = (unsigned short *)tmpbuf;
113 for(i=0;i<0x10;i++)
Alexander Grafef9e6de2016-04-11 16:16:14 +0200114 chksum += le16_to_cpu(chksumbuf[i]);
wdenkcc1c8a12002-11-02 22:58:18 +0000115 if(chksum!=0) {
116 if(verb)
Simon Glassbcce53d2016-02-29 15:25:51 -0700117 printf("** Checksum Error in booting catalog validation entry on %d:%d **\n",
Simon Glass06e293e2023-08-24 13:55:28 -0600118 desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000119 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 Glass06e293e2023-08-24 13:55:28 -0600124 desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000125 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 Glass06e293e2023-08-24 13:55:28 -0600131 desc->devnum, part_num);
wdenkcc1c8a12002-11-02 22:58:18 +0000132 return(-1);
133 }
134#endif
135 /* the validation entry seems to be ok, now search the "partition" */
Alexander Graf2579c672016-04-11 16:16:15 +0200136 entry_num=1;
wdenkcc1c8a12002-11-02 22:58:18 +0000137 offset=0x20;
Ben Whitten192bc692015-12-30 13:05:58 +0000138 strcpy((char *)info->type, "U-Boot");
Simon Glass06e293e2023-08-24 13:55:28 -0600139 part_set_generic_name(desc, part_num, (char *)info->name);
wdenkcc1c8a12002-11-02 22:58:18 +0000140 /* 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 Glass06e293e2023-08-24 13:55:28 -0600161 part_num, desc->devnum);
wdenkcc1c8a12002-11-02 22:58:18 +0000162 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 Glass06e293e2023-08-24 13:55:28 -0600169 part_num, desc->devnum);
wdenkcc1c8a12002-11-02 22:58:18 +0000170 return(-1);
171found:
172 if(pide->boot_ind!=0x88) {
173 if(verb)
Simon Glassbcce53d2016-02-29 15:25:51 -0700174 printf("** Partition %d is not bootable on device %d **\n",
Simon Glass06e293e2023-08-24 13:55:28 -0600175 part_num, desc->devnum);
wdenkcc1c8a12002-11-02 22:58:18 +0000176 return (-1);
177 }
178 switch(pide->boot_media) {
179 case 0x00: /* no emulation */
Simon Glass09192282016-03-16 07:45:34 -0600180 info->size = get_unaligned_le16(pide->sec_cnt)>>2;
wdenkcc1c8a12002-11-02 22:58:18 +0000181 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 Glass09192282016-03-16 07:45:34 -0600188 newblkaddr = get_unaligned_le32(pide->rel_block_addr);
wdenkcc1c8a12002-11-02 22:58:18 +0000189 info->start=newblkaddr;
Alexander Grafa2adb172016-04-11 16:16:16 +0200190
Simon Glass06e293e2023-08-24 13:55:28 -0600191 if (desc->blksz == 512) {
Alexander Grafa2adb172016-04-11 16:16:16 +0200192 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);
wdenkcc1c8a12002-11-02 22:58:18 +0000198 return 0;
199}
200
Simon Glass06e293e2023-08-24 13:55:28 -0600201static int part_get_info_iso(struct blk_desc *desc, int part_num,
Simon Glass05289792020-05-10 11:39:57 -0600202 struct disk_partition *info)
wdenkcc1c8a12002-11-02 22:58:18 +0000203{
Simon Glass06e293e2023-08-24 13:55:28 -0600204 return part_get_info_iso_verb(desc, part_num, info, 0);
wdenkcc1c8a12002-11-02 22:58:18 +0000205}
206
Simon Glass06e293e2023-08-24 13:55:28 -0600207static void part_print_iso(struct blk_desc *desc)
wdenkcc1c8a12002-11-02 22:58:18 +0000208{
Simon Glass05289792020-05-10 11:39:57 -0600209 struct disk_partition info;
wdenkcc1c8a12002-11-02 22:58:18 +0000210 int i;
Simon Glass3e8bd462016-02-29 15:25:48 -0700211
Simon Glass06e293e2023-08-24 13:55:28 -0600212 if (part_get_info_iso_verb(desc, 1, &info, 0) == -1) {
Simon Glassbcce53d2016-02-29 15:25:51 -0700213 printf("** No boot partition found on device %d **\n",
Simon Glass06e293e2023-08-24 13:55:28 -0600214 desc->devnum);
wdenkcc1c8a12002-11-02 22:58:18 +0000215 return;
216 }
217 printf("Part Start Sect x Size Type\n");
Alexander Graf28f00142016-07-21 01:31:56 +0200218 i=1;
wdenkcc1c8a12002-11-02 22:58:18 +0000219 do {
Heinrich Schuchardtd9d88492021-08-06 00:51:46 +0200220 printf(" %2d %8" LBAFlength "u %8" LBAFlength "u %6ld %.32s\n",
Frederic Leroy04735e92013-06-26 18:11:25 +0200221 i, info.start, info.size, info.blksz, info.type);
wdenkcc1c8a12002-11-02 22:58:18 +0000222 i++;
Simon Glass06e293e2023-08-24 13:55:28 -0600223 } while (part_get_info_iso_verb(desc, i, &info, 0) != -1);
wdenkcc1c8a12002-11-02 22:58:18 +0000224}
225
Simon Glass06e293e2023-08-24 13:55:28 -0600226static int part_test_iso(struct blk_desc *desc)
wdenkcc1c8a12002-11-02 22:58:18 +0000227{
Simon Glass05289792020-05-10 11:39:57 -0600228 struct disk_partition info;
wdenkcc1c8a12002-11-02 22:58:18 +0000229
Simon Glass06e293e2023-08-24 13:55:28 -0600230 return part_get_info_iso_verb(desc, 1, &info, 0);
wdenkcc1c8a12002-11-02 22:58:18 +0000231}
232
Simon Glass96e5b032016-02-29 15:25:47 -0700233U_BOOT_PART_TYPE(iso) = {
234 .name = "ISO",
235 .part_type = PART_TYPE_ISO,
Petr Kulhavy87b85302016-09-09 10:27:15 +0200236 .max_entries = ISO_ENTRY_NUMBERS,
Simon Glass3e8bd462016-02-29 15:25:48 -0700237 .get_info = part_get_info_iso,
Simon Glass084bf4c2016-02-29 15:26:04 -0700238 .print = part_print_iso,
239 .test = part_test_iso,
Simon Glass96e5b032016-02-29 15:25:47 -0700240};