part_iso.c 7.12 KB
Newer Older
wdenk's avatar
wdenk committed
1 2 3 4
/*
 * (C) Copyright 2001
 * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch.
 *
5
 * SPDX-License-Identifier:	GPL-2.0+
wdenk's avatar
wdenk committed
6 7 8 9
 */

#include <common.h>
#include <command.h>
10
#include <asm/unaligned.h>
wdenk's avatar
wdenk committed
11 12
#include "part_iso.h"

13
#ifdef HAVE_BLOCK_DEVICE
wdenk's avatar
wdenk committed
14

wdenk's avatar
wdenk committed
15
/* #define	ISO_PART_DEBUG */
wdenk's avatar
wdenk committed
16 17 18 19 20 21 22 23 24 25 26 27 28

#ifdef	ISO_PART_DEBUG
#define	PRINTF(fmt,args...)	printf (fmt ,##args)
#else
#define PRINTF(fmt,args...)
#endif

/* enable this if CDs are written with the PowerPC Platform ID */
#undef CHECK_FOR_POWERPC_PLATTFORM
#define CD_SECTSIZE 2048

static unsigned char tmpbuf[CD_SECTSIZE];

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
unsigned long iso_dread(struct blk_desc *block_dev, lbaint_t start,
                        lbaint_t blkcnt, void *buffer)
{
	unsigned long ret;

	if (block_dev->blksz == 512) {
		/* Convert from 2048 to 512 sector size */
		start *= 4;
		blkcnt *= 4;
	}

	ret = blk_dread(block_dev, start, blkcnt, buffer);

	if (block_dev->blksz == 512)
		ret /= 4;

	return ret;
}

wdenk's avatar
wdenk committed
48
/* only boot records will be listed as valid partitions */
49 50
int part_get_info_iso_verb(struct blk_desc *dev_desc, int part_num,
			   disk_partition_t *info, int verb)
wdenk's avatar
wdenk committed
51 52 53 54 55 56 57 58 59 60
{
	int i,offset,entry_num;
	unsigned short *chksumbuf;
	unsigned short chksum;
	unsigned long newblkaddr,blkaddr,lastsect,bootaddr;
	iso_boot_rec_t *pbr = (iso_boot_rec_t	*)tmpbuf; /* boot record */
	iso_pri_rec_t *ppr = (iso_pri_rec_t	*)tmpbuf;	/* primary desc */
	iso_val_entry_t *pve = (iso_val_entry_t *)tmpbuf;
	iso_init_def_entry_t *pide;

61
	if ((dev_desc->blksz != CD_SECTSIZE) && (dev_desc->blksz != 512))
62 63
		return -1;

wdenk's avatar
wdenk committed
64 65
	/* the first sector (sector 0x10) must be a primary volume desc */
	blkaddr=PVD_OFFSET;
66
	if (iso_dread(dev_desc, PVD_OFFSET, 1, (ulong *)tmpbuf) != 1)
67
		return -1;
wdenk's avatar
wdenk committed
68 69 70
	if(ppr->desctype!=0x01) {
		if(verb)
			printf ("** First descriptor is NOT a primary desc on %d:%d **\n",
71
				dev_desc->devnum, part_num);
wdenk's avatar
wdenk committed
72 73
		return (-1);
	}
Wolfgang Denk's avatar
Wolfgang Denk committed
74
	if(strncmp((char *)ppr->stand_ident,"CD001",5)!=0) {
wdenk's avatar
wdenk committed
75 76
		if(verb)
			printf ("** Wrong ISO Ident: %s on %d:%d **\n",
77
				ppr->stand_ident, dev_desc->devnum, part_num);
wdenk's avatar
wdenk committed
78 79
		return (-1);
	}
80 81 82
	lastsect = le32_to_cpu(ppr->firstsek_LEpathtab1_LE);
	/* assuming same block size for all entries */
	info->blksz = be16_to_cpu(ppr->secsize_BE);
wdenk's avatar
wdenk committed
83 84
	PRINTF(" Lastsect:%08lx\n",lastsect);
	for(i=blkaddr;i<lastsect;i++) {
85
		PRINTF("Reading block %d\n", i);
86
		if (iso_dread(dev_desc, i, 1, (ulong *)tmpbuf) != 1)
87
			return -1;
wdenk's avatar
wdenk committed
88 89 90 91 92
		if(ppr->desctype==0x00)
			break; /* boot entry found */
		if(ppr->desctype==0xff) {
			if(verb)
				printf ("** No valid boot catalog found on %d:%d **\n",
93
					dev_desc->devnum, part_num);
wdenk's avatar
wdenk committed
94 95 96
			return (-1);
		}
	}
Wolfgang Denk's avatar
Wolfgang Denk committed
97
	/* boot entry found */
wdenk's avatar
wdenk committed
98 99 100
	if(strncmp(pbr->ident_str,"EL TORITO SPECIFICATION",23)!=0) {
		if(verb)
			printf ("** Wrong El Torito ident: %s on %d:%d **\n",
101
				pbr->ident_str, dev_desc->devnum, part_num);
wdenk's avatar
wdenk committed
102 103
		return (-1);
	}
104
	bootaddr = get_unaligned_le32(pbr->pointer);
wdenk's avatar
wdenk committed
105
	PRINTF(" Boot Entry at: %08lX\n",bootaddr);
106
	if (iso_dread(dev_desc, bootaddr, 1, (ulong *)tmpbuf) != 1) {
wdenk's avatar
wdenk committed
107 108
		if(verb)
			printf ("** Can't read Boot Entry at %lX on %d:%d **\n",
109
				bootaddr, dev_desc->devnum, part_num);
wdenk's avatar
wdenk committed
110 111 112 113 114
		return (-1);
	}
	chksum=0;
	chksumbuf = (unsigned short *)tmpbuf;
	for(i=0;i<0x10;i++)
115
		chksum += le16_to_cpu(chksumbuf[i]);
wdenk's avatar
wdenk committed
116 117
	if(chksum!=0) {
		if(verb)
118 119
			printf("** Checksum Error in booting catalog validation entry on %d:%d **\n",
			       dev_desc->devnum, part_num);
wdenk's avatar
wdenk committed
120 121 122 123 124
		return (-1);
	}
	if((pve->key[0]!=0x55)||(pve->key[1]!=0xAA)) {
		if(verb)
			printf ("** Key 0x55 0xAA error on %d:%d **\n",
125
				dev_desc->devnum, part_num);
wdenk's avatar
wdenk committed
126 127 128 129 130 131
		return(-1);
	}
#ifdef CHECK_FOR_POWERPC_PLATTFORM
	if(pve->platform!=0x01) {
		if(verb)
			printf ("** No PowerPC platform CD on %d:%d **\n",
132
				dev_desc->devnum, part_num);
wdenk's avatar
wdenk committed
133 134 135 136
		return(-1);
	}
#endif
	/* the validation entry seems to be ok, now search the "partition" */
Alexander Graf's avatar
Alexander Graf committed
137
	entry_num=1;
wdenk's avatar
wdenk committed
138
	offset=0x20;
139
	strcpy((char *)info->type, "U-Boot");
wdenk's avatar
wdenk committed
140 141
	switch(dev_desc->if_type) {
		case IF_TYPE_IDE:
142
		case IF_TYPE_SATA:
wdenk's avatar
wdenk committed
143
		case IF_TYPE_ATAPI:
144
			sprintf ((char *)info->name, "hd%c%d",
145
				'a' + dev_desc->devnum, part_num);
wdenk's avatar
wdenk committed
146 147
			break;
		case IF_TYPE_SCSI:
148
			sprintf ((char *)info->name, "sd%c%d",
149
				'a' + dev_desc->devnum, part_num);
wdenk's avatar
wdenk committed
150 151
			break;
		case IF_TYPE_USB:
152
			sprintf ((char *)info->name, "usbd%c%d",
153
				'a' + dev_desc->devnum, part_num);
wdenk's avatar
wdenk committed
154 155
			break;
		case IF_TYPE_DOC:
156
			sprintf ((char *)info->name, "docd%c%d",
157
				'a' + dev_desc->devnum, part_num);
wdenk's avatar
wdenk committed
158 159
			break;
		default:
160
			sprintf ((char *)info->name, "xx%c%d",
161
				'a' + dev_desc->devnum, part_num);
wdenk's avatar
wdenk committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
			break;
	}
	/* the bootcatalog (including validation Entry) is limited to 2048Bytes
	 * (63 boot entries + validation entry) */
	 while(offset<2048) {
		pide=(iso_init_def_entry_t *)&tmpbuf[offset];
		if ((pide->boot_ind==0x88) ||
		    (pide->boot_ind==0x00)) { /* Header Id for default Sections Entries */
			if(entry_num==part_num) { /* part found */
				goto found;
			}
			entry_num++; /* count partitions Entries (boot and non bootables */
			offset+=0x20;
			continue;
		}
		if ((pide->boot_ind==0x90) ||	/* Section Header Entry */
		    (pide->boot_ind==0x91) ||	/* Section Header Entry (last) */
		    (pide->boot_ind==0x44)) {	/* Extension Indicator */
			offset+=0x20; /* skip unused entries */
		}
		else {
			if(verb)
				printf ("** Partition %d not found on device %d **\n",
185
					part_num, dev_desc->devnum);
wdenk's avatar
wdenk committed
186 187 188 189 190 191 192
			return(-1);
		}
	}
	/* if we reach this point entire sector has been
	 * searched w/o succsess */
	if(verb)
		printf ("** Partition %d not found on device %d **\n",
193
			part_num, dev_desc->devnum);
wdenk's avatar
wdenk committed
194 195 196 197
	return(-1);
found:
	if(pide->boot_ind!=0x88) {
		if(verb)
198 199
			printf("** Partition %d is not bootable on device %d **\n",
			       part_num, dev_desc->devnum);
wdenk's avatar
wdenk committed
200 201 202 203
		return (-1);
	}
	switch(pide->boot_media) {
		case 0x00: /* no emulation */
204
			info->size = get_unaligned_le16(pide->sec_cnt)>>2;
wdenk's avatar
wdenk committed
205 206 207 208 209 210 211
			break;
		case 0x01:	info->size=2400>>2; break; /* 1.2MByte Floppy */
		case 0x02:	info->size=2880>>2; break; /* 1.44MByte Floppy */
		case 0x03:	info->size=5760>>2; break; /* 2.88MByte Floppy */
		case 0x04:	info->size=2880>>2; break; /* dummy (HD Emulation) */
		default:	info->size=0; break;
	}
212
	newblkaddr = get_unaligned_le32(pide->rel_block_addr);
wdenk's avatar
wdenk committed
213
	info->start=newblkaddr;
214 215 216 217 218 219 220 221

	if (dev_desc->blksz == 512) {
		info->size *= 4;
		info->start *= 4;
		info->blksz = 512;
	}

	PRINTF(" part %d found @ %lx size %lx\n",part_num,info->start,info->size);
wdenk's avatar
wdenk committed
222 223 224
	return 0;
}

225
static int part_get_info_iso(struct blk_desc *dev_desc, int part_num,
226
				  disk_partition_t *info)
wdenk's avatar
wdenk committed
227
{
228
	return part_get_info_iso_verb(dev_desc, part_num, info, 1);
wdenk's avatar
wdenk committed
229 230
}

231
static void part_print_iso(struct blk_desc *dev_desc)
wdenk's avatar
wdenk committed
232 233 234
{
	disk_partition_t info;
	int i;
235 236

	if (part_get_info_iso_verb(dev_desc, 0, &info, 0) == -1) {
237 238
		printf("** No boot partition found on device %d **\n",
		       dev_desc->devnum);
wdenk's avatar
wdenk committed
239 240 241 242 243
		return;
	}
	printf("Part   Start     Sect x Size Type\n");
	i=0;
	do {
244 245
		printf(" %2d " LBAFU " " LBAFU " %6ld %.32s\n",
		       i, info.start, info.size, info.blksz, info.type);
wdenk's avatar
wdenk committed
246
		i++;
247
	} while (part_get_info_iso_verb(dev_desc, i, &info, 0) != -1);
wdenk's avatar
wdenk committed
248 249
}

250
static int part_test_iso(struct blk_desc *dev_desc)
wdenk's avatar
wdenk committed
251 252 253
{
	disk_partition_t info;

Alexander Graf's avatar
Alexander Graf committed
254
	return part_get_info_iso_verb(dev_desc, 1, &info, 1);
wdenk's avatar
wdenk committed
255 256
}

257 258 259
U_BOOT_PART_TYPE(iso) = {
	.name		= "ISO",
	.part_type	= PART_TYPE_ISO,
260
	.get_info	= part_get_info_iso,
261 262
	.print		= part_print_iso,
	.test		= part_test_iso,
263
};
264
#endif