1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate /* 30*0Sstevel@tonic-gate * Basic file system reading code for standalone I/O system. 31*0Sstevel@tonic-gate * Simulates a primitive UNIX I/O system (read(), write(), open(), etc). 32*0Sstevel@tonic-gate * Does not support writes. 33*0Sstevel@tonic-gate */ 34*0Sstevel@tonic-gate 35*0Sstevel@tonic-gate #include <sys/param.h> 36*0Sstevel@tonic-gate #include <sys/sysmacros.h> 37*0Sstevel@tonic-gate #include <sys/vnode.h> 38*0Sstevel@tonic-gate #include <sys/fs/ufs_fsdir.h> 39*0Sstevel@tonic-gate #include <sys/fs/ufs_fs.h> 40*0Sstevel@tonic-gate #include <sys/fs/ufs_inode.h> 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gate #include <sys/fs/hsfs_spec.h> 43*0Sstevel@tonic-gate #include <sys/fs/hsfs_isospec.h> 44*0Sstevel@tonic-gate #include <sys/fs/hsfs_node.h> 45*0Sstevel@tonic-gate #include <sys/fs/hsfs_susp.h> 46*0Sstevel@tonic-gate #include <sys/fs/hsfs_rrip.h> 47*0Sstevel@tonic-gate #include <sys/bootvfs.h> 48*0Sstevel@tonic-gate #include <sys/filep.h> 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate #ifdef _BOOT 51*0Sstevel@tonic-gate #include "../common/util.h" 52*0Sstevel@tonic-gate #else 53*0Sstevel@tonic-gate #include <sys/sunddi.h> 54*0Sstevel@tonic-gate #endif 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate #define hdbtodb(n) ((ISO_SECTOR_SIZE / DEV_BSIZE) * (n)) 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate #define HSFS_NUM_SIG 14 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate #define SUSP_SP_IX 0 61*0Sstevel@tonic-gate #define SUSP_CE_IX 1 62*0Sstevel@tonic-gate #define SUSP_PD_IX 2 63*0Sstevel@tonic-gate #define SUSP_ST_IX 3 64*0Sstevel@tonic-gate #define SUSP_ER_IX 4 65*0Sstevel@tonic-gate #define RRIP_PX_IX 5 66*0Sstevel@tonic-gate #define RRIP_PN_IX 6 67*0Sstevel@tonic-gate #define RRIP_SL_IX 7 68*0Sstevel@tonic-gate #define RRIP_CL_IX 8 69*0Sstevel@tonic-gate #define RRIP_PL_IX 9 70*0Sstevel@tonic-gate #define RRIP_RE_IX 10 71*0Sstevel@tonic-gate #define RRIP_RF_IX 11 72*0Sstevel@tonic-gate #define RRIP_RR_IX 12 73*0Sstevel@tonic-gate #define RRIP_NM_IX 13 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate #ifdef _BOOT 76*0Sstevel@tonic-gate #define dprintf if (bootrd_debug) printf 77*0Sstevel@tonic-gate #else 78*0Sstevel@tonic-gate #define printf kobj_printf 79*0Sstevel@tonic-gate #define dprintf if (bootrd_debug) kobj_printf 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gate /* PRINTFLIKE1 */ 82*0Sstevel@tonic-gate extern void kobj_printf(char *, ...); 83*0Sstevel@tonic-gate #endif 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate extern int bootrd_debug; 86*0Sstevel@tonic-gate extern void *bkmem_alloc(size_t); 87*0Sstevel@tonic-gate extern void bkmem_free(void *, size_t); 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gate struct dirstuff { 90*0Sstevel@tonic-gate int loc; 91*0Sstevel@tonic-gate fileid_t *filep; 92*0Sstevel@tonic-gate }; 93*0Sstevel@tonic-gate 94*0Sstevel@tonic-gate struct hs_direct { 95*0Sstevel@tonic-gate struct direct hs_ufs_dir; 96*0Sstevel@tonic-gate struct hs_direntry hs_dir; 97*0Sstevel@tonic-gate }; 98*0Sstevel@tonic-gate 99*0Sstevel@tonic-gate static uint_t root_ino = 0; 100*0Sstevel@tonic-gate static struct hs_volume *hsfsp; 101*0Sstevel@tonic-gate static fileid_t *head; 102*0Sstevel@tonic-gate 103*0Sstevel@tonic-gate static char *hsfs_sig_tab[] = { 104*0Sstevel@tonic-gate SUSP_SP, 105*0Sstevel@tonic-gate SUSP_CE, 106*0Sstevel@tonic-gate SUSP_PD, 107*0Sstevel@tonic-gate SUSP_ST, 108*0Sstevel@tonic-gate SUSP_ER, 109*0Sstevel@tonic-gate RRIP_PX, 110*0Sstevel@tonic-gate RRIP_PN, 111*0Sstevel@tonic-gate RRIP_SL, 112*0Sstevel@tonic-gate RRIP_CL, 113*0Sstevel@tonic-gate RRIP_PL, 114*0Sstevel@tonic-gate RRIP_RE, 115*0Sstevel@tonic-gate RRIP_TF, 116*0Sstevel@tonic-gate RRIP_RR, 117*0Sstevel@tonic-gate RRIP_NM 118*0Sstevel@tonic-gate }; 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gate static int hsfs_num_sig = sizeof (hsfs_sig_tab) / sizeof (hsfs_sig_tab[0]); 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gate /* 123*0Sstevel@tonic-gate * Local prototypes 124*0Sstevel@tonic-gate */ 125*0Sstevel@tonic-gate static struct hs_direct *readdir(struct dirstuff *); 126*0Sstevel@tonic-gate static uint_t parse_dir(fileid_t *, int, struct hs_direct *); 127*0Sstevel@tonic-gate static uint_t parse_susp(char *, uint_t *, struct hs_direct *); 128*0Sstevel@tonic-gate static ino_t dlook(char *, fileid_t *); 129*0Sstevel@tonic-gate static int opendir(ino_t, fileid_t *); 130*0Sstevel@tonic-gate static ino_t find(char *, fileid_t *); 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate static int bhsfs_mountroot(char *str); 133*0Sstevel@tonic-gate static int bhsfs_unmountroot(void); 134*0Sstevel@tonic-gate static int bhsfs_open(char *str, int flags); 135*0Sstevel@tonic-gate static int bhsfs_close(int fd); 136*0Sstevel@tonic-gate static void bhsfs_closeall(void); 137*0Sstevel@tonic-gate static ssize_t bhsfs_read(int fdesc, char *buf, size_t count); 138*0Sstevel@tonic-gate static off_t bhsfs_lseek(int fdesc, off_t addr, int whence); 139*0Sstevel@tonic-gate 140*0Sstevel@tonic-gate static fileid_t * 141*0Sstevel@tonic-gate find_fp(int fd) 142*0Sstevel@tonic-gate { 143*0Sstevel@tonic-gate fileid_t *filep = head; 144*0Sstevel@tonic-gate 145*0Sstevel@tonic-gate if (fd >= 0) { 146*0Sstevel@tonic-gate while ((filep = filep->fi_forw) != head) 147*0Sstevel@tonic-gate if (fd == filep->fi_filedes) 148*0Sstevel@tonic-gate return (filep->fi_taken ? filep : 0); 149*0Sstevel@tonic-gate } 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gate return (0); 152*0Sstevel@tonic-gate } 153*0Sstevel@tonic-gate 154*0Sstevel@tonic-gate static int 155*0Sstevel@tonic-gate opendir(ino_t inode, fileid_t *filep) 156*0Sstevel@tonic-gate { 157*0Sstevel@tonic-gate struct hs_direct hsdep; 158*0Sstevel@tonic-gate 159*0Sstevel@tonic-gate dprintf("opendir: inode = %ld\n", inode); 160*0Sstevel@tonic-gate /* Set up the IO request */ 161*0Sstevel@tonic-gate filep->fi_offset = 0; 162*0Sstevel@tonic-gate filep->fi_blocknum = hdbtodb(inode); 163*0Sstevel@tonic-gate filep->fi_count = ISO_SECTOR_SIZE; 164*0Sstevel@tonic-gate filep->fi_memp = 0; 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate if (diskread(filep)) 167*0Sstevel@tonic-gate return (0); 168*0Sstevel@tonic-gate 169*0Sstevel@tonic-gate filep->fi_offset = 0; 170*0Sstevel@tonic-gate filep->fi_blocknum = hdbtodb(inode); 171*0Sstevel@tonic-gate 172*0Sstevel@tonic-gate if (inode != root_ino) 173*0Sstevel@tonic-gate return (0); 174*0Sstevel@tonic-gate 175*0Sstevel@tonic-gate if (parse_dir(filep, 0, &hsdep) > 0) { 176*0Sstevel@tonic-gate struct inode *ip; 177*0Sstevel@tonic-gate 178*0Sstevel@tonic-gate ip = filep->fi_inode; 179*0Sstevel@tonic-gate if (ip == NULL) 180*0Sstevel@tonic-gate ip = filep->fi_inode = bkmem_alloc(sizeof (*ip)); 181*0Sstevel@tonic-gate 182*0Sstevel@tonic-gate ip->i_size = hsdep.hs_dir.ext_size; 183*0Sstevel@tonic-gate ip->i_smode = hsdep.hs_dir.mode; 184*0Sstevel@tonic-gate ip->i_number = inode; 185*0Sstevel@tonic-gate return (0); 186*0Sstevel@tonic-gate } 187*0Sstevel@tonic-gate return (1); 188*0Sstevel@tonic-gate } 189*0Sstevel@tonic-gate 190*0Sstevel@tonic-gate static ino_t 191*0Sstevel@tonic-gate find(char *path, fileid_t *filep) 192*0Sstevel@tonic-gate { 193*0Sstevel@tonic-gate char *q; 194*0Sstevel@tonic-gate char c; 195*0Sstevel@tonic-gate ino_t n; 196*0Sstevel@tonic-gate 197*0Sstevel@tonic-gate dprintf("find: %s\n", path); 198*0Sstevel@tonic-gate if (path == NULL || *path == '\0') 199*0Sstevel@tonic-gate return (0); 200*0Sstevel@tonic-gate 201*0Sstevel@tonic-gate if (opendir(root_ino, filep)) 202*0Sstevel@tonic-gate return (0); 203*0Sstevel@tonic-gate 204*0Sstevel@tonic-gate while (*path) { 205*0Sstevel@tonic-gate while (*path == '/') 206*0Sstevel@tonic-gate path++; 207*0Sstevel@tonic-gate q = path; 208*0Sstevel@tonic-gate while (*q != '/' && *q != '\0') 209*0Sstevel@tonic-gate q++; 210*0Sstevel@tonic-gate c = *q; 211*0Sstevel@tonic-gate *q = '\0'; 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate if ((n = dlook(path, filep)) != 0) { 214*0Sstevel@tonic-gate if (c == '\0') 215*0Sstevel@tonic-gate break; 216*0Sstevel@tonic-gate if (opendir(n, filep)) 217*0Sstevel@tonic-gate return (0); 218*0Sstevel@tonic-gate *q = c; 219*0Sstevel@tonic-gate path = q; 220*0Sstevel@tonic-gate continue; 221*0Sstevel@tonic-gate } else { 222*0Sstevel@tonic-gate return (0); 223*0Sstevel@tonic-gate } 224*0Sstevel@tonic-gate } 225*0Sstevel@tonic-gate return ((ino_t)n); 226*0Sstevel@tonic-gate } 227*0Sstevel@tonic-gate 228*0Sstevel@tonic-gate static ino_t 229*0Sstevel@tonic-gate dlook(char *s, fileid_t *filep) 230*0Sstevel@tonic-gate { 231*0Sstevel@tonic-gate struct hs_direct *hsdep; 232*0Sstevel@tonic-gate struct direct *udp; 233*0Sstevel@tonic-gate struct inode *ip; 234*0Sstevel@tonic-gate struct dirstuff dirp; 235*0Sstevel@tonic-gate int len; 236*0Sstevel@tonic-gate 237*0Sstevel@tonic-gate dprintf("dlook: %s\n", s); 238*0Sstevel@tonic-gate ip = filep->fi_inode; 239*0Sstevel@tonic-gate if (s == NULL || *s == '\0') 240*0Sstevel@tonic-gate return (0); 241*0Sstevel@tonic-gate if ((ip->i_smode & IFMT) != IFDIR) { 242*0Sstevel@tonic-gate return (0); 243*0Sstevel@tonic-gate } 244*0Sstevel@tonic-gate if (ip->i_size == 0) { 245*0Sstevel@tonic-gate return (0); 246*0Sstevel@tonic-gate } 247*0Sstevel@tonic-gate len = strlen(s); 248*0Sstevel@tonic-gate dirp.loc = 0; 249*0Sstevel@tonic-gate dirp.filep = filep; 250*0Sstevel@tonic-gate for (hsdep = readdir(&dirp); hsdep != NULL; hsdep = readdir(&dirp)) { 251*0Sstevel@tonic-gate udp = &hsdep->hs_ufs_dir; 252*0Sstevel@tonic-gate if (udp->d_namlen == 1 && 253*0Sstevel@tonic-gate udp->d_name[0] == '.' && 254*0Sstevel@tonic-gate udp->d_name[1] == '\0') 255*0Sstevel@tonic-gate continue; 256*0Sstevel@tonic-gate if (udp->d_namlen == 2 && 257*0Sstevel@tonic-gate udp->d_name[0] == '.' && 258*0Sstevel@tonic-gate udp->d_name[1] == '.' && 259*0Sstevel@tonic-gate udp->d_name[2] == '\0') 260*0Sstevel@tonic-gate continue; 261*0Sstevel@tonic-gate if (udp->d_namlen == len && (strcmp(s, udp->d_name)) == 0) { 262*0Sstevel@tonic-gate struct inode *ip = filep->fi_inode; 263*0Sstevel@tonic-gate 264*0Sstevel@tonic-gate filep->fi_offset = 0; 265*0Sstevel@tonic-gate filep->fi_blocknum = hdbtodb(udp->d_ino); 266*0Sstevel@tonic-gate 267*0Sstevel@tonic-gate bzero(filep->fi_inode, sizeof (struct inode)); 268*0Sstevel@tonic-gate ip->i_size = hsdep->hs_dir.ext_size; 269*0Sstevel@tonic-gate ip->i_smode = hsdep->hs_dir.mode; 270*0Sstevel@tonic-gate ip->i_number = udp->d_ino; 271*0Sstevel@tonic-gate return (udp->d_ino); 272*0Sstevel@tonic-gate } 273*0Sstevel@tonic-gate } 274*0Sstevel@tonic-gate return (0); 275*0Sstevel@tonic-gate } 276*0Sstevel@tonic-gate 277*0Sstevel@tonic-gate /* 278*0Sstevel@tonic-gate * get next entry in a directory. 279*0Sstevel@tonic-gate */ 280*0Sstevel@tonic-gate static struct hs_direct * 281*0Sstevel@tonic-gate readdir(struct dirstuff *dirp) 282*0Sstevel@tonic-gate { 283*0Sstevel@tonic-gate static struct hs_direct hsdep; 284*0Sstevel@tonic-gate struct direct *udp = &hsdep.hs_ufs_dir; 285*0Sstevel@tonic-gate struct inode *ip; 286*0Sstevel@tonic-gate fileid_t *filep; 287*0Sstevel@tonic-gate daddr_t lbn; 288*0Sstevel@tonic-gate int off; 289*0Sstevel@tonic-gate 290*0Sstevel@tonic-gate dprintf("readdir: start\n"); 291*0Sstevel@tonic-gate filep = dirp->filep; 292*0Sstevel@tonic-gate ip = filep->fi_inode; 293*0Sstevel@tonic-gate for (;;) { 294*0Sstevel@tonic-gate if (dirp->loc >= ip->i_size) { 295*0Sstevel@tonic-gate return (NULL); 296*0Sstevel@tonic-gate } 297*0Sstevel@tonic-gate off = dirp->loc & ((1 << ISO_SECTOR_SHIFT) - 1); 298*0Sstevel@tonic-gate if (off == 0) { 299*0Sstevel@tonic-gate lbn = hdbtodb(dirp->loc >> ISO_SECTOR_SHIFT); 300*0Sstevel@tonic-gate filep->fi_blocknum = lbn + hdbtodb(ip->i_number); 301*0Sstevel@tonic-gate filep->fi_count = ISO_SECTOR_SIZE; 302*0Sstevel@tonic-gate filep->fi_memp = 0; 303*0Sstevel@tonic-gate if (diskread(filep)) { 304*0Sstevel@tonic-gate dprintf("readdir: diskread failed\n"); 305*0Sstevel@tonic-gate return (NULL); 306*0Sstevel@tonic-gate } 307*0Sstevel@tonic-gate } 308*0Sstevel@tonic-gate dirp->loc += parse_dir(filep, off, &hsdep); 309*0Sstevel@tonic-gate if (udp->d_reclen == 0 && dirp->loc <= ip->i_size) { 310*0Sstevel@tonic-gate dirp->loc = roundup(dirp->loc, ISO_SECTOR_SIZE); 311*0Sstevel@tonic-gate continue; 312*0Sstevel@tonic-gate } 313*0Sstevel@tonic-gate return (&hsdep); 314*0Sstevel@tonic-gate } 315*0Sstevel@tonic-gate } 316*0Sstevel@tonic-gate 317*0Sstevel@tonic-gate static int 318*0Sstevel@tonic-gate getblock(fileid_t *filep) 319*0Sstevel@tonic-gate { 320*0Sstevel@tonic-gate struct inode *ip = filep->fi_inode; 321*0Sstevel@tonic-gate int off, size, diff; 322*0Sstevel@tonic-gate daddr_t lbn; 323*0Sstevel@tonic-gate 324*0Sstevel@tonic-gate dprintf("getblock: start\n"); 325*0Sstevel@tonic-gate diff = ip->i_size - filep->fi_offset; 326*0Sstevel@tonic-gate if (diff <= 0) 327*0Sstevel@tonic-gate return (-1); 328*0Sstevel@tonic-gate 329*0Sstevel@tonic-gate /* which block (or frag) in the file do we read? */ 330*0Sstevel@tonic-gate lbn = hdbtodb(filep->fi_offset >> ISO_SECTOR_SHIFT); 331*0Sstevel@tonic-gate filep->fi_blocknum = lbn + hdbtodb(ip->i_number); 332*0Sstevel@tonic-gate 333*0Sstevel@tonic-gate off = filep->fi_offset & ((1 << ISO_SECTOR_SHIFT) - 1); 334*0Sstevel@tonic-gate size = filep->fi_count = ISO_SECTOR_SIZE; 335*0Sstevel@tonic-gate filep->fi_memp = 0; 336*0Sstevel@tonic-gate if (diskread(filep)) /* Trap errors */ 337*0Sstevel@tonic-gate return (-1); 338*0Sstevel@tonic-gate 339*0Sstevel@tonic-gate if (filep->fi_offset - off + size >= ip->i_size) 340*0Sstevel@tonic-gate filep->fi_count = diff + off; 341*0Sstevel@tonic-gate filep->fi_count -= off; 342*0Sstevel@tonic-gate filep->fi_memp += off; 343*0Sstevel@tonic-gate dprintf("getblock: end\n"); 344*0Sstevel@tonic-gate return (0); 345*0Sstevel@tonic-gate } 346*0Sstevel@tonic-gate 347*0Sstevel@tonic-gate static ssize_t 348*0Sstevel@tonic-gate bhsfs_read(int fd, caddr_t buf, size_t count) 349*0Sstevel@tonic-gate { 350*0Sstevel@tonic-gate int i, j; 351*0Sstevel@tonic-gate fileid_t *filep; 352*0Sstevel@tonic-gate struct inode *ip; 353*0Sstevel@tonic-gate caddr_t n; 354*0Sstevel@tonic-gate 355*0Sstevel@tonic-gate dprintf("bhsfs_read %d, count 0x%lx\n", fd, count); 356*0Sstevel@tonic-gate filep = find_fp(fd); 357*0Sstevel@tonic-gate if (filep == NULL) 358*0Sstevel@tonic-gate return (-1); 359*0Sstevel@tonic-gate 360*0Sstevel@tonic-gate ip = filep->fi_inode; 361*0Sstevel@tonic-gate n = buf; 362*0Sstevel@tonic-gate if (filep->fi_offset + count > ip->i_size) 363*0Sstevel@tonic-gate count = ip->i_size - filep->fi_offset; 364*0Sstevel@tonic-gate 365*0Sstevel@tonic-gate if ((i = count) <= 0) 366*0Sstevel@tonic-gate return (0); 367*0Sstevel@tonic-gate 368*0Sstevel@tonic-gate while (i > 0) { 369*0Sstevel@tonic-gate if (filep->fi_count <= 0) { 370*0Sstevel@tonic-gate if (getblock(filep) == -1) 371*0Sstevel@tonic-gate return (0); 372*0Sstevel@tonic-gate } 373*0Sstevel@tonic-gate j = MIN(i, filep->fi_count); 374*0Sstevel@tonic-gate bcopy(filep->fi_memp, buf, (uint_t)j); 375*0Sstevel@tonic-gate buf += j; 376*0Sstevel@tonic-gate filep->fi_memp += j; 377*0Sstevel@tonic-gate filep->fi_offset += j; 378*0Sstevel@tonic-gate filep->fi_count -= j; 379*0Sstevel@tonic-gate i -= j; 380*0Sstevel@tonic-gate } 381*0Sstevel@tonic-gate 382*0Sstevel@tonic-gate dprintf("bhsfs_read: read 0x%x\n", (int)(buf - n)); 383*0Sstevel@tonic-gate return (buf - n); 384*0Sstevel@tonic-gate } 385*0Sstevel@tonic-gate 386*0Sstevel@tonic-gate /*ARGSUSED*/ 387*0Sstevel@tonic-gate static int 388*0Sstevel@tonic-gate bhsfs_mountroot(char *str) 389*0Sstevel@tonic-gate { 390*0Sstevel@tonic-gate char *bufp; 391*0Sstevel@tonic-gate 392*0Sstevel@tonic-gate if (hsfsp != NULL) 393*0Sstevel@tonic-gate return (0); /* already mounted */ 394*0Sstevel@tonic-gate 395*0Sstevel@tonic-gate dprintf("mounting ramdisk as hsfs\n"); 396*0Sstevel@tonic-gate 397*0Sstevel@tonic-gate hsfsp = bkmem_alloc(sizeof (*hsfsp)); 398*0Sstevel@tonic-gate bzero(hsfsp, sizeof (*hsfsp)); 399*0Sstevel@tonic-gate head = bkmem_alloc(sizeof (*head)); 400*0Sstevel@tonic-gate bzero(head, sizeof (*head)); 401*0Sstevel@tonic-gate head->fi_back = head->fi_forw = head; 402*0Sstevel@tonic-gate 403*0Sstevel@tonic-gate /* now read the superblock. */ 404*0Sstevel@tonic-gate head->fi_blocknum = hdbtodb(ISO_VOLDESC_SEC); 405*0Sstevel@tonic-gate head->fi_offset = 0; 406*0Sstevel@tonic-gate head->fi_count = ISO_SECTOR_SIZE; 407*0Sstevel@tonic-gate head->fi_memp = head->fi_buf; 408*0Sstevel@tonic-gate if (diskread(head)) { 409*0Sstevel@tonic-gate printf("failed to read superblock\n"); 410*0Sstevel@tonic-gate bhsfs_closeall(); 411*0Sstevel@tonic-gate return (-1); 412*0Sstevel@tonic-gate } 413*0Sstevel@tonic-gate 414*0Sstevel@tonic-gate /* Since RRIP is based on ISO9660, that's where we start */ 415*0Sstevel@tonic-gate bufp = head->fi_buf; 416*0Sstevel@tonic-gate if ((ISO_DESC_TYPE(bufp) != ISO_VD_PVD) || 417*0Sstevel@tonic-gate (strncmp((const char *)ISO_std_id(bufp), ISO_ID_STRING, 418*0Sstevel@tonic-gate ISO_ID_STRLEN) != 0) || (ISO_STD_VER(bufp) != ISO_ID_VER)) { 419*0Sstevel@tonic-gate dprintf("volume type does not match\n"); 420*0Sstevel@tonic-gate bhsfs_closeall(); 421*0Sstevel@tonic-gate return (-1); 422*0Sstevel@tonic-gate } 423*0Sstevel@tonic-gate 424*0Sstevel@tonic-gate /* Now we fill in the volume descriptor */ 425*0Sstevel@tonic-gate hsfsp->vol_size = ISO_VOL_SIZE(bufp); 426*0Sstevel@tonic-gate hsfsp->lbn_size = ISO_BLK_SIZE(bufp); 427*0Sstevel@tonic-gate hsfsp->lbn_shift = ISO_SECTOR_SHIFT; 428*0Sstevel@tonic-gate hsfsp->lbn_secshift = ISO_SECTOR_SHIFT; 429*0Sstevel@tonic-gate hsfsp->vol_set_size = (ushort_t)ISO_SET_SIZE(bufp); 430*0Sstevel@tonic-gate hsfsp->vol_set_seq = (ushort_t)ISO_SET_SEQ(bufp); 431*0Sstevel@tonic-gate 432*0Sstevel@tonic-gate /* Make sure we have a valid logical block size */ 433*0Sstevel@tonic-gate if (hsfsp->lbn_size & ~(1 << hsfsp->lbn_shift)) { 434*0Sstevel@tonic-gate printf("%d invalid logical block size\n", hsfsp->lbn_size); 435*0Sstevel@tonic-gate bhsfs_closeall(); 436*0Sstevel@tonic-gate return (-1); 437*0Sstevel@tonic-gate } 438*0Sstevel@tonic-gate 439*0Sstevel@tonic-gate /* Since an HSFS root could be located anywhere on the media! */ 440*0Sstevel@tonic-gate root_ino = IDE_EXT_LBN(ISO_root_dir(bufp)); 441*0Sstevel@tonic-gate return (0); 442*0Sstevel@tonic-gate } 443*0Sstevel@tonic-gate 444*0Sstevel@tonic-gate static int 445*0Sstevel@tonic-gate bhsfs_unmountroot(void) 446*0Sstevel@tonic-gate { 447*0Sstevel@tonic-gate if (hsfsp == NULL) 448*0Sstevel@tonic-gate return (-1); 449*0Sstevel@tonic-gate 450*0Sstevel@tonic-gate bhsfs_closeall(); 451*0Sstevel@tonic-gate 452*0Sstevel@tonic-gate return (0); 453*0Sstevel@tonic-gate } 454*0Sstevel@tonic-gate 455*0Sstevel@tonic-gate /* 456*0Sstevel@tonic-gate * Open a file. 457*0Sstevel@tonic-gate */ 458*0Sstevel@tonic-gate /*ARGSUSED*/ 459*0Sstevel@tonic-gate int 460*0Sstevel@tonic-gate bhsfs_open(char *str, int flags) 461*0Sstevel@tonic-gate { 462*0Sstevel@tonic-gate static int filedes = 1; 463*0Sstevel@tonic-gate 464*0Sstevel@tonic-gate fileid_t *filep; 465*0Sstevel@tonic-gate ino_t ino; 466*0Sstevel@tonic-gate 467*0Sstevel@tonic-gate dprintf("open %s\n", str); 468*0Sstevel@tonic-gate filep = (fileid_t *)bkmem_alloc(sizeof (fileid_t)); 469*0Sstevel@tonic-gate filep->fi_back = head->fi_back; 470*0Sstevel@tonic-gate filep->fi_forw = head; 471*0Sstevel@tonic-gate head->fi_back->fi_forw = filep; 472*0Sstevel@tonic-gate head->fi_back = filep; 473*0Sstevel@tonic-gate filep->fi_filedes = filedes++; 474*0Sstevel@tonic-gate filep->fi_taken = 1; 475*0Sstevel@tonic-gate filep->fi_path = (char *)bkmem_alloc(strlen(str) + 1); 476*0Sstevel@tonic-gate (void) strcpy(filep->fi_path, str); 477*0Sstevel@tonic-gate filep->fi_inode = NULL; 478*0Sstevel@tonic-gate bzero(filep->fi_buf, MAXBSIZE); 479*0Sstevel@tonic-gate 480*0Sstevel@tonic-gate ino = find(str, filep); 481*0Sstevel@tonic-gate if (ino == 0) { 482*0Sstevel@tonic-gate (void) bhsfs_close(filep->fi_filedes); 483*0Sstevel@tonic-gate return (-1); 484*0Sstevel@tonic-gate } 485*0Sstevel@tonic-gate 486*0Sstevel@tonic-gate filep->fi_blocknum = hdbtodb(ino); 487*0Sstevel@tonic-gate filep->fi_offset = 0; 488*0Sstevel@tonic-gate filep->fi_count = 0; 489*0Sstevel@tonic-gate filep->fi_memp = 0; 490*0Sstevel@tonic-gate 491*0Sstevel@tonic-gate dprintf("open done\n"); 492*0Sstevel@tonic-gate return (filep->fi_filedes); 493*0Sstevel@tonic-gate } 494*0Sstevel@tonic-gate 495*0Sstevel@tonic-gate int 496*0Sstevel@tonic-gate bhsfs_close(int fd) 497*0Sstevel@tonic-gate { 498*0Sstevel@tonic-gate fileid_t *filep; 499*0Sstevel@tonic-gate 500*0Sstevel@tonic-gate dprintf("close %d\n", fd); 501*0Sstevel@tonic-gate if (!(filep = find_fp(fd))) 502*0Sstevel@tonic-gate return (-1); 503*0Sstevel@tonic-gate 504*0Sstevel@tonic-gate if (filep->fi_taken == 0 || filep == head) { 505*0Sstevel@tonic-gate printf("File descripter %d no allocated!\n", fd); 506*0Sstevel@tonic-gate return (-1); 507*0Sstevel@tonic-gate } 508*0Sstevel@tonic-gate 509*0Sstevel@tonic-gate /* unlink and deallocate node */ 510*0Sstevel@tonic-gate filep->fi_forw->fi_back = filep->fi_back; 511*0Sstevel@tonic-gate filep->fi_back->fi_forw = filep->fi_forw; 512*0Sstevel@tonic-gate if (filep->fi_inode) 513*0Sstevel@tonic-gate bkmem_free(filep->fi_inode, sizeof (struct inode)); 514*0Sstevel@tonic-gate bkmem_free(filep->fi_path, strlen(filep->fi_path) + 1); 515*0Sstevel@tonic-gate bkmem_free((char *)filep, sizeof (fileid_t)); 516*0Sstevel@tonic-gate dprintf("close done\n"); 517*0Sstevel@tonic-gate return (0); 518*0Sstevel@tonic-gate } 519*0Sstevel@tonic-gate 520*0Sstevel@tonic-gate static void 521*0Sstevel@tonic-gate bhsfs_closeall(void) 522*0Sstevel@tonic-gate { 523*0Sstevel@tonic-gate fileid_t *filep; 524*0Sstevel@tonic-gate 525*0Sstevel@tonic-gate while ((filep = head->fi_forw) != head) 526*0Sstevel@tonic-gate if (filep->fi_taken && bhsfs_close(filep->fi_filedes)) 527*0Sstevel@tonic-gate printf("Filesystem may be inconsistent.\n"); 528*0Sstevel@tonic-gate 529*0Sstevel@tonic-gate bkmem_free(hsfsp, sizeof (*hsfsp)); 530*0Sstevel@tonic-gate bkmem_free(head, sizeof (fileid_t)); 531*0Sstevel@tonic-gate hsfsp = NULL; 532*0Sstevel@tonic-gate head = NULL; 533*0Sstevel@tonic-gate } 534*0Sstevel@tonic-gate 535*0Sstevel@tonic-gate /* 536*0Sstevel@tonic-gate * This version of seek() only performs absolute seeks (whence == 0). 537*0Sstevel@tonic-gate */ 538*0Sstevel@tonic-gate static off_t 539*0Sstevel@tonic-gate bhsfs_lseek(int fd, off_t addr, int whence) 540*0Sstevel@tonic-gate { 541*0Sstevel@tonic-gate fileid_t *filep; 542*0Sstevel@tonic-gate 543*0Sstevel@tonic-gate dprintf("lseek %d, off = %lx\n", fd, addr); 544*0Sstevel@tonic-gate if (!(filep = find_fp(fd))) 545*0Sstevel@tonic-gate return (-1); 546*0Sstevel@tonic-gate 547*0Sstevel@tonic-gate switch (whence) { 548*0Sstevel@tonic-gate case SEEK_CUR: 549*0Sstevel@tonic-gate filep->fi_offset += addr; 550*0Sstevel@tonic-gate break; 551*0Sstevel@tonic-gate case SEEK_SET: 552*0Sstevel@tonic-gate filep->fi_offset = addr; 553*0Sstevel@tonic-gate break; 554*0Sstevel@tonic-gate default: 555*0Sstevel@tonic-gate case SEEK_END: 556*0Sstevel@tonic-gate printf("lseek(): invalid whence value %d\n", whence); 557*0Sstevel@tonic-gate break; 558*0Sstevel@tonic-gate } 559*0Sstevel@tonic-gate 560*0Sstevel@tonic-gate filep->fi_blocknum = addr / DEV_BSIZE; 561*0Sstevel@tonic-gate filep->fi_count = 0; 562*0Sstevel@tonic-gate return (0); 563*0Sstevel@tonic-gate } 564*0Sstevel@tonic-gate 565*0Sstevel@tonic-gate /* 566*0Sstevel@tonic-gate * Parse a directory entry. 567*0Sstevel@tonic-gate * 568*0Sstevel@tonic-gate */ 569*0Sstevel@tonic-gate static uint_t 570*0Sstevel@tonic-gate parse_dir(fileid_t *filep, int offset, struct hs_direct *hsdep) 571*0Sstevel@tonic-gate { 572*0Sstevel@tonic-gate char *bufp = (char *)(filep->fi_memp + offset); 573*0Sstevel@tonic-gate struct direct *udp = &hsdep->hs_ufs_dir; /* ufs-style dir info */ 574*0Sstevel@tonic-gate struct hs_direntry *hdp = &hsdep->hs_dir; /* hsfs-style dir info */ 575*0Sstevel@tonic-gate uint_t ce_lbn; 576*0Sstevel@tonic-gate uint_t ce_len; 577*0Sstevel@tonic-gate uint_t nmlen; 578*0Sstevel@tonic-gate uint_t i; 579*0Sstevel@tonic-gate uchar_t c; 580*0Sstevel@tonic-gate 581*0Sstevel@tonic-gate dprintf("parse_dir: offset = %d\n", offset); 582*0Sstevel@tonic-gate /* a zero length dir entry terminates the dir block */ 583*0Sstevel@tonic-gate udp->d_reclen = IDE_DIR_LEN(bufp); 584*0Sstevel@tonic-gate if (udp->d_reclen == 0) 585*0Sstevel@tonic-gate return (0); 586*0Sstevel@tonic-gate 587*0Sstevel@tonic-gate /* fill in some basic hsfs info */ 588*0Sstevel@tonic-gate hdp->ext_lbn = IDE_EXT_LBN(bufp); 589*0Sstevel@tonic-gate hdp->ext_size = IDE_EXT_SIZE(bufp); 590*0Sstevel@tonic-gate hdp->xar_len = IDE_XAR_LEN(bufp); 591*0Sstevel@tonic-gate hdp->intlf_sz = IDE_INTRLV_SIZE(bufp); 592*0Sstevel@tonic-gate hdp->intlf_sk = IDE_INTRLV_SKIP(bufp); 593*0Sstevel@tonic-gate hdp->sym_link = NULL; 594*0Sstevel@tonic-gate 595*0Sstevel@tonic-gate /* we use lbn of data extent as an inode # equivalent */ 596*0Sstevel@tonic-gate udp->d_ino = hdp->ext_lbn; 597*0Sstevel@tonic-gate 598*0Sstevel@tonic-gate c = IDE_FLAGS(bufp); 599*0Sstevel@tonic-gate if (IDE_REGULAR_FILE(c)) { 600*0Sstevel@tonic-gate hdp->type = VREG; 601*0Sstevel@tonic-gate hdp->mode = IFREG; 602*0Sstevel@tonic-gate hdp->nlink = 1; 603*0Sstevel@tonic-gate } else if (IDE_REGULAR_DIR(c)) { 604*0Sstevel@tonic-gate hdp->type = VDIR; 605*0Sstevel@tonic-gate hdp->mode = IFDIR; 606*0Sstevel@tonic-gate hdp->nlink = 2; 607*0Sstevel@tonic-gate } else { 608*0Sstevel@tonic-gate printf("pd(): file type=0x%x unknown.\n", c); 609*0Sstevel@tonic-gate } 610*0Sstevel@tonic-gate 611*0Sstevel@tonic-gate /* 612*0Sstevel@tonic-gate * Massage hsfs name, recognizing special entries for . and .. 613*0Sstevel@tonic-gate * else lopping off version junk. 614*0Sstevel@tonic-gate */ 615*0Sstevel@tonic-gate 616*0Sstevel@tonic-gate /* Some initial conditions */ 617*0Sstevel@tonic-gate nmlen = IDE_NAME_LEN(bufp); 618*0Sstevel@tonic-gate c = *IDE_NAME(bufp); 619*0Sstevel@tonic-gate /* Special Case: Current Directory */ 620*0Sstevel@tonic-gate if (nmlen == 1 && c == '\0') { 621*0Sstevel@tonic-gate udp->d_name[0] = '.'; 622*0Sstevel@tonic-gate udp->d_name[1] = '\0'; 623*0Sstevel@tonic-gate udp->d_namlen = 1; 624*0Sstevel@tonic-gate /* Special Case: Parent Directory */ 625*0Sstevel@tonic-gate } else if (nmlen == 1 && c == '\001') { 626*0Sstevel@tonic-gate udp->d_name[0] = '.'; 627*0Sstevel@tonic-gate udp->d_name[1] = '.'; 628*0Sstevel@tonic-gate udp->d_name[2] = '\0'; 629*0Sstevel@tonic-gate udp->d_namlen = 2; 630*0Sstevel@tonic-gate /* Other file name */ 631*0Sstevel@tonic-gate } else { 632*0Sstevel@tonic-gate udp->d_namlen = 0; 633*0Sstevel@tonic-gate for (i = 0; i < nmlen; i++) { 634*0Sstevel@tonic-gate c = *(IDE_name(bufp)+i); 635*0Sstevel@tonic-gate if (c == ';') 636*0Sstevel@tonic-gate break; 637*0Sstevel@tonic-gate else if (c == ' ') 638*0Sstevel@tonic-gate continue; 639*0Sstevel@tonic-gate else 640*0Sstevel@tonic-gate udp->d_name[udp->d_namlen++] = c; 641*0Sstevel@tonic-gate } 642*0Sstevel@tonic-gate udp->d_name[udp->d_namlen] = '\0'; 643*0Sstevel@tonic-gate } 644*0Sstevel@tonic-gate 645*0Sstevel@tonic-gate /* System Use Fields */ 646*0Sstevel@tonic-gate ce_len = IDE_SUA_LEN(bufp); 647*0Sstevel@tonic-gate 648*0Sstevel@tonic-gate if (ce_len <= 0) 649*0Sstevel@tonic-gate return (udp->d_reclen); 650*0Sstevel@tonic-gate 651*0Sstevel@tonic-gate /* there is an SUA for this dir entry; go parse it */ 652*0Sstevel@tonic-gate ce_lbn = parse_susp((char *)IDE_sys_use_area(bufp), &ce_len, hsdep); 653*0Sstevel@tonic-gate 654*0Sstevel@tonic-gate if (ce_lbn) { 655*0Sstevel@tonic-gate /* 656*0Sstevel@tonic-gate * store away current position in dir, 657*0Sstevel@tonic-gate * as we will be using the iobuf to reading SUA. 658*0Sstevel@tonic-gate */ 659*0Sstevel@tonic-gate daddr_t save_bn = filep->fi_blocknum; 660*0Sstevel@tonic-gate daddr_t save_offset = filep->fi_offset; 661*0Sstevel@tonic-gate caddr_t save_ma = filep->fi_memp; 662*0Sstevel@tonic-gate int save_cc = filep->fi_count; 663*0Sstevel@tonic-gate do { 664*0Sstevel@tonic-gate filep->fi_count = ISO_SECTOR_SIZE; 665*0Sstevel@tonic-gate filep->fi_offset = 0; 666*0Sstevel@tonic-gate filep->fi_blocknum = hdbtodb(ce_lbn); 667*0Sstevel@tonic-gate filep->fi_memp = 0; 668*0Sstevel@tonic-gate if (diskread(filep)) { 669*0Sstevel@tonic-gate printf("failed to read cont. area\n"); 670*0Sstevel@tonic-gate ce_len = 0; 671*0Sstevel@tonic-gate ce_lbn = 0; 672*0Sstevel@tonic-gate break; 673*0Sstevel@tonic-gate } 674*0Sstevel@tonic-gate ce_lbn = parse_susp(filep->fi_memp, &ce_len, 675*0Sstevel@tonic-gate hsdep); 676*0Sstevel@tonic-gate } while (ce_lbn); 677*0Sstevel@tonic-gate filep->fi_count = save_cc; 678*0Sstevel@tonic-gate filep->fi_offset = save_offset; 679*0Sstevel@tonic-gate filep->fi_blocknum = save_bn; 680*0Sstevel@tonic-gate filep->fi_memp = save_ma; 681*0Sstevel@tonic-gate } 682*0Sstevel@tonic-gate return (udp->d_reclen); 683*0Sstevel@tonic-gate } 684*0Sstevel@tonic-gate 685*0Sstevel@tonic-gate /* 686*0Sstevel@tonic-gate * Parse the System Use Fields in this System Use Area. 687*0Sstevel@tonic-gate * Return blk number of continuation/SUA, or 0 if no continuation/not a SUA. 688*0Sstevel@tonic-gate */ 689*0Sstevel@tonic-gate static uint_t 690*0Sstevel@tonic-gate parse_susp(char *bufp, uint_t *len, struct hs_direct *hsdep) 691*0Sstevel@tonic-gate { 692*0Sstevel@tonic-gate struct direct *udp = &hsdep->hs_ufs_dir; /* ufs-style info */ 693*0Sstevel@tonic-gate char *susp; 694*0Sstevel@tonic-gate uint_t cur_off = 0; 695*0Sstevel@tonic-gate uint_t blk_len = *len; 696*0Sstevel@tonic-gate uint_t susp_len = 0; 697*0Sstevel@tonic-gate uint_t ce_lbn = 0; 698*0Sstevel@tonic-gate uint_t i; 699*0Sstevel@tonic-gate 700*0Sstevel@tonic-gate dprintf("parse_susp: len = %d\n", *len); 701*0Sstevel@tonic-gate while (cur_off < blk_len) { 702*0Sstevel@tonic-gate susp = (char *)(bufp + cur_off); 703*0Sstevel@tonic-gate 704*0Sstevel@tonic-gate /* 705*0Sstevel@tonic-gate * A null entry, or an entry with zero length 706*0Sstevel@tonic-gate * terminates the SUSP. 707*0Sstevel@tonic-gate */ 708*0Sstevel@tonic-gate if (susp[0] == '\0' || susp[1] == '\0' || 709*0Sstevel@tonic-gate (susp_len = SUF_LEN(susp)) == 0) 710*0Sstevel@tonic-gate break; 711*0Sstevel@tonic-gate 712*0Sstevel@tonic-gate /* 713*0Sstevel@tonic-gate * Compare current entry to all known signatures. 714*0Sstevel@tonic-gate */ 715*0Sstevel@tonic-gate for (i = 0; i < hsfs_num_sig; i++) 716*0Sstevel@tonic-gate if (strncmp(hsfs_sig_tab[i], susp, SUF_SIG_LEN) == 0) 717*0Sstevel@tonic-gate break; 718*0Sstevel@tonic-gate switch (i) { 719*0Sstevel@tonic-gate case SUSP_CE_IX: 720*0Sstevel@tonic-gate /* 721*0Sstevel@tonic-gate * CE signature: continuation of SUSP. 722*0Sstevel@tonic-gate * will want to return new lbn, len. 723*0Sstevel@tonic-gate */ 724*0Sstevel@tonic-gate ce_lbn = CE_BLK_LOC(susp); 725*0Sstevel@tonic-gate *len = CE_CONT_LEN(susp); 726*0Sstevel@tonic-gate break; 727*0Sstevel@tonic-gate case RRIP_NM_IX: 728*0Sstevel@tonic-gate /* NM signature: POSIX-style file name */ 729*0Sstevel@tonic-gate if (!RRIP_NAME_FLAGS(susp)) { 730*0Sstevel@tonic-gate udp->d_namlen = RRIP_NAME_LEN(susp); 731*0Sstevel@tonic-gate bcopy((char *)RRIP_name(susp), 732*0Sstevel@tonic-gate udp->d_name, udp->d_namlen); 733*0Sstevel@tonic-gate udp->d_name[udp->d_namlen] = '\0'; 734*0Sstevel@tonic-gate } 735*0Sstevel@tonic-gate break; 736*0Sstevel@tonic-gate case HSFS_NUM_SIG: 737*0Sstevel@tonic-gate /* couldn't find a legit susp, terminate loop */ 738*0Sstevel@tonic-gate case SUSP_ST_IX: 739*0Sstevel@tonic-gate /* ST signature: terminates SUSP */ 740*0Sstevel@tonic-gate return (ce_lbn); 741*0Sstevel@tonic-gate case SUSP_SP_IX: 742*0Sstevel@tonic-gate case RRIP_RR_IX: 743*0Sstevel@tonic-gate default: 744*0Sstevel@tonic-gate break; 745*0Sstevel@tonic-gate } 746*0Sstevel@tonic-gate cur_off += susp_len; 747*0Sstevel@tonic-gate } 748*0Sstevel@tonic-gate return (ce_lbn); 749*0Sstevel@tonic-gate } 750*0Sstevel@tonic-gate 751*0Sstevel@tonic-gate struct boot_fs_ops bhsfs_ops = { 752*0Sstevel@tonic-gate "boot_hsfs", 753*0Sstevel@tonic-gate bhsfs_mountroot, 754*0Sstevel@tonic-gate bhsfs_unmountroot, 755*0Sstevel@tonic-gate bhsfs_open, 756*0Sstevel@tonic-gate bhsfs_close, 757*0Sstevel@tonic-gate bhsfs_read, 758*0Sstevel@tonic-gate bhsfs_lseek, 759*0Sstevel@tonic-gate NULL 760*0Sstevel@tonic-gate }; 761