2 * Copyright © 1997 Pluto Technologies International, Inc. Boulder CO
3 * Copyright © 1997 interface business GmbH, Dresden.
6 * This code was written by Jörg Wunsch, Dresden.
7 * Direct comments to <joerg_wunsch@interface-business.de>.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * $FreeBSD: src/sys/i386/boot/cdboot/cdrom.c,v 1.4 1999/08/28 00:43:17 peter Exp $
36 #include <isofs/cd9660/iso.h>
38 #define BLKSIZE 2048 /* CD-ROM data block size */
39 #define BIOSSEC 512 /* BIOS sector size */
41 #define CD2LBA(rba) ((rba) << 2) /* CD-ROM relative block to BIOS LBA */
43 u_int32_t sessionstart;
45 static struct iso_primary_descriptor pdesc;
47 static char *rootdirbuf;
48 static size_t rootdirsize;
49 static char xbuf[BLKSIZE];
50 static u_int32_t curblk, startblk, filesize, offset;
52 static int bread(u_int32_t rba, size_t nblks, void *buf);
53 static void badread(const char *msg, u_int32_t blkno);
54 static struct iso_directory_record *find(const char *path, int list_only);
55 static char *get_rr_name(struct iso_directory_record *dirp, size_t *len_ret);
56 static int iread(u_char *buf, size_t len,
57 void (*copyfun)(const void *src, void *dst, size_t size));
59 static struct daddrpacket dpkt = { 0x10 };
62 devopen(u_int32_t session)
66 struct iso_directory_record *rootdirp;
68 if ((rv = bread(session + 16, 1, &pdesc)) != 0) {
69 printf("Error reading primary ISO descriptor: %d\n", rv);
72 rootdirp = (struct iso_directory_record *)pdesc.root_directory_record;
73 rootdirblk = isonum_733(rootdirp->extent);
74 rootdirsize = isonum_733(rootdirp->size);
76 /* just in case, round up */
77 rootdirsize = (rootdirsize + BLKSIZE - 1) & ~(BLKSIZE - 1);
79 if (rootdirbuf != NULL)
81 if ((rootdirbuf = malloc(rootdirsize)) == 0) {
82 printf("Cannot allocate memory for the root "
83 "directory buffer.\n");
86 if ((rv = bread(rootdirblk, rootdirsize / BLKSIZE, rootdirbuf))
88 printf("Error reading root directory: %d\n", rv);
92 DPRINTF(("Root directory is 0x%x bytes @ %d\n",
93 rootdirsize, rootdirblk));
99 bread(u_int32_t rba, size_t nblks, void *buf)
103 for (i = 0, rv = -1; rv != 0 && i < 3; i++) {
104 dpkt.nblocks = nblks * (BLKSIZE / BIOSSEC);
105 dpkt.boffs = (u_int16_t)((int)buf & 0xffff);
107 dpkt.lba = CD2LBA(rba);
110 DPRINTF(("Calling biosreadlba(%d blocks, lba %d) = ",
111 dpkt.nblocks, dpkt.lba));
114 rv = biosreadlba(&dpkt);
117 DPRINTF(("%d\n", rv));
131 badread(const char *msg, u_int32_t blkno)
133 printf("Error reading block %d from CD-ROM: %s\n",
137 static __inline size_t
138 minlen(size_t a, size_t b)
144 * Internal form of read()/xread().
147 iread(u_char *buf, size_t len,
148 void (*copyfun)(const void *src, void *dst, size_t size))
150 u_int32_t newblk, ptr;
153 newblk = offset / BLKSIZE + startblk;
155 if (newblk != curblk) {
156 if (offset + len >= filesize) {
157 badread("access beyond file limit", newblk);
160 if (bread(newblk, 1, xbuf)) {
161 badread("BIOS read error", newblk);
166 ptr = offset & (BLKSIZE - 1);
168 /* initial short transfer */
169 bsize = minlen(BLKSIZE - ptr, len);
170 copyfun(xbuf + ptr, buf, bsize);
175 for (; len > 0; len -= bsize) {
176 bsize = minlen(len, BLKSIZE);
177 newblk = offset / BLKSIZE + startblk;
179 if (newblk != curblk) {
180 if (offset + bsize > filesize) {
181 badread("access beyond file limit", newblk);
184 if (bread(newblk, 1, xbuf)) {
185 badread("BIOS read error", newblk);
190 copyfun(xbuf, buf, bsize);
198 read(u_char *buf, size_t len)
200 DPRINTF(("read(0x%x, %d)\n", (int)buf, len));
201 return iread(buf, len, bcopy);
205 xread(u_char *buf, size_t len)
207 DPRINTF(("xread(0x%x, %d)\n", (int)buf, len));
208 return iread(buf, len, pcpy);
212 get_rr_name(struct iso_directory_record *dirp, size_t *len_ret)
219 struct rr_nm_header {
220 struct rr_header rrh;
222 char name[0]; /* XXX -- using gcc extension */
226 cp = dirp->name + (u_char)dirp->name_len[0];
227 /* round up to 16-bit boundary; ugly */
228 cp = (char *)(((int)cp + 1) & ~1);
229 rrp = (struct rr_header *)cp;
231 if (rrp->type[0] != 'R' || rrp->type[1] != 'R') {
232 DPRINTF(("no RR, "));
236 DPRINTF(("RR attribs: "));
238 while (cp - (char *)dirp <= (u_char)dirp->length[0]) {
239 rrp = (struct rr_header *)cp;
240 DPRINTF(("%c%c ", rrp->type[0], rrp->type[1]));
241 if (rrp->type[0] == 'N' && rrp->type[1] == 'M') {
242 rrnmp = (struct rr_nm_header *)rrp;
243 *len_ret = rrp->len - sizeof(struct rr_nm_header);
252 static struct iso_directory_record *
253 find(const char *path, int list_only)
255 struct iso_directory_record *dirp;
257 size_t len, entrylen;
260 int (*comp)(const char *, const char *);
262 while (*path && *path == '/')
265 for (ptr = rootdirbuf, i = 1;
266 ptr < rootdirbuf + rootdirsize;
267 ptr += entrylen, i++) {
268 dirp = (struct iso_directory_record *)ptr;
269 entrylen = (u_char)dirp->length[0];
270 len = (u_char)dirp->name_len[0];
272 DPRINTF(("# %d: offset 0x%x, length 0x%x = %d, ",
273 i, (int)(ptr - rootdirbuf), entrylen, entrylen));
277 * Dir entry of length 0. That's the last
278 * entry in this block, advance to the next
279 * block (if any). In case we get beyond the
280 * end of the directory, we'll fall off the
281 * loop due to the rootdirsize condition in
282 * the `for' statement.
284 DPRINTF(("entrylen 0\n"));
285 entrylen = (~((ptr - rootdirbuf) + BLKSIZE - 1))
290 DPRINTF(("name_len 0\n"));
294 (dirp->name[0] == '\0' || dirp->name[1] == '\1')) {
295 DPRINTF(("dot/dot-dot entry\n"));
298 /* don't consider directories */
299 if (dirp->flags[0] & 2) {
300 DPRINTF(("directory\n"));
303 rrname = get_rr_name(dirp, &len);
304 comp = rrname? strcmp: strcasecmp;
306 bcopy(rrname? rrname: dirp->name, namebuf, len);
308 DPRINTF(("name `%s'\n", namebuf));
312 printf("%s ", namebuf);
314 } else if (comp(path, namebuf) == 0)
329 u_int32_t oldsession;
330 int session, list_only;
331 struct iso_directory_record *dirp;
337 * We accept the following boot string:
339 * [@sessionstart] name
341 for (cp = name; *cp; cp++)
343 /* we don't support filenames with spaces */
349 printf("Syntax error\n");
353 oldsession = sessionstart;
357 case '0': case '1': case '2':
358 case '3': case '4': case '5':
359 case '6': case '7': case '8':
363 sessionstart += *cp - '0';
374 if (session && devopen(sessionstart) == -1) {
375 (void)devopen(oldsession);
376 sessionstart = oldsession;
379 /* XXX no filename, only session arg */
382 list_only = fname[0] == '?' && fname[1] == 0;
384 DPRINTF(("Calling find(%s, %d):\n", fname, list_only));
385 dirp = find(fname, list_only);
386 DPRINTF(("find() returned 0x%x\n", (int)dirp));
393 startblk = isonum_733(dirp->extent);
394 filesize = isonum_733(dirp->size);
396 DPRINTF(("startblk = %d, filesize = %d\n", startblk, filesize));
398 curblk = 0; /* force a re-read, 0 is impossible file start */