17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5986fd29aSsetje * Common Development and Distribution License (the "License").
6986fd29aSsetje * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22986fd29aSsetje * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include <sys/param.h>
277c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
287c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fsdir.h>
297c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
307c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
317c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
327c478bd9Sstevel@tonic-gate #include <sys/promif.h>
337c478bd9Sstevel@tonic-gate #include <sys/filep.h>
347c478bd9Sstevel@tonic-gate #include <sys/salib.h>
357c478bd9Sstevel@tonic-gate #include <sys/sacache.h>
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate #include <sys/fs/hsfs_spec.h>
387c478bd9Sstevel@tonic-gate #include <sys/fs/hsfs_isospec.h>
397c478bd9Sstevel@tonic-gate #include <sys/fs/hsfs_node.h>
407c478bd9Sstevel@tonic-gate #include <sys/fs/hsfs_susp.h>
417c478bd9Sstevel@tonic-gate #include <sys/fs/hsfs_rrip.h>
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate #include "hsfs_sig.h"
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate #include <sys/stat.h>
467c478bd9Sstevel@tonic-gate #include <sys/bootvfs.h>
477c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
487c478bd9Sstevel@tonic-gate #include <sys/bootdebug.h>
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate #define hdbtodb(n) ((ISO_SECTOR_SIZE / DEV_BSIZE) * (n))
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate #define THE_EPOCH 1970
537c478bd9Sstevel@tonic-gate #define END_OF_TIME 2099
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate /* May not need this... */
567c478bd9Sstevel@tonic-gate static uint_t sua_offset = 0;
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate /* The root inode on an HSFS filesystem can be anywhere! */
597c478bd9Sstevel@tonic-gate static uint_t root_ino = 0; /* This is both a flag and a value */
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate static fileid_t *head;
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate /* Only got one of these...ergo, only 1 fs open at once */
647c478bd9Sstevel@tonic-gate static devid_t *devp;
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate struct dirinfo {
677c478bd9Sstevel@tonic-gate int loc;
687c478bd9Sstevel@tonic-gate fileid_t *fi;
697c478bd9Sstevel@tonic-gate };
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate struct hs_direct {
727c478bd9Sstevel@tonic-gate struct direct hs_ufs_dir;
737c478bd9Sstevel@tonic-gate struct hs_direntry hs_dir;
747c478bd9Sstevel@tonic-gate };
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate * Function prototypes
787c478bd9Sstevel@tonic-gate */
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate static int boot_hsfs_mountroot(char *str);
817c478bd9Sstevel@tonic-gate static int boot_hsfs_unmountroot(void);
827c478bd9Sstevel@tonic-gate static int boot_hsfs_open(char *filename, int flags);
837c478bd9Sstevel@tonic-gate static int boot_hsfs_close(int fd);
847c478bd9Sstevel@tonic-gate static ssize_t boot_hsfs_read(int fd, caddr_t buf, size_t size);
857c478bd9Sstevel@tonic-gate static off_t boot_hsfs_lseek(int, off_t, int);
867c478bd9Sstevel@tonic-gate static int boot_hsfs_fstat(int fd, struct bootstat *stp);
877c478bd9Sstevel@tonic-gate static void boot_hsfs_closeall(int flag);
887c478bd9Sstevel@tonic-gate static int boot_hsfs_getdents(int fd, struct dirent *dep, unsigned size);
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate struct boot_fs_ops boot_hsfs_ops = {
917c478bd9Sstevel@tonic-gate "hsfs",
927c478bd9Sstevel@tonic-gate boot_hsfs_mountroot,
937c478bd9Sstevel@tonic-gate boot_hsfs_unmountroot,
947c478bd9Sstevel@tonic-gate boot_hsfs_open,
957c478bd9Sstevel@tonic-gate boot_hsfs_close,
967c478bd9Sstevel@tonic-gate boot_hsfs_read,
977c478bd9Sstevel@tonic-gate boot_hsfs_lseek,
987c478bd9Sstevel@tonic-gate boot_hsfs_fstat,
997c478bd9Sstevel@tonic-gate boot_hsfs_closeall,
1007c478bd9Sstevel@tonic-gate boot_hsfs_getdents
1017c478bd9Sstevel@tonic-gate };
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate static ino_t find(fileid_t *, char *);
1047c478bd9Sstevel@tonic-gate static ino_t dlook(fileid_t *, char *);
1057c478bd9Sstevel@tonic-gate static int opendir(fileid_t *, ino_t);
1067c478bd9Sstevel@tonic-gate static struct hs_direct *readdir(struct dirinfo *);
1077c478bd9Sstevel@tonic-gate static uint_t parse_dir(fileid_t *, int, struct hs_direct *);
1087c478bd9Sstevel@tonic-gate static uint_t parse_susp(char *, uint_t *, struct hs_direct *);
1097c478bd9Sstevel@tonic-gate static void hs_seti(fileid_t *, struct hs_direct *, ino_t);
1107c478bd9Sstevel@tonic-gate static void hs_dodates(enum hs_vol_type, struct hs_direntry *, char *);
1117c478bd9Sstevel@tonic-gate static time_t hs_date_to_gmtime(int, int, int, int);
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate * There is only 1 open (mounted) device at any given time.
1157c478bd9Sstevel@tonic-gate * So we can keep a single, global devp file descriptor to
1167c478bd9Sstevel@tonic-gate * use to index into the di[] array. This is not true for the
1177c478bd9Sstevel@tonic-gate * fi[] array. We can have more than one file open at once,
1187c478bd9Sstevel@tonic-gate * so there is no global fd for the fi[].
1197c478bd9Sstevel@tonic-gate * The user program must save the fd passed back from open()
1207c478bd9Sstevel@tonic-gate * and use it to do subsequent read()'s.
1217c478bd9Sstevel@tonic-gate */
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate static int
opendir(fileid_t * filep,ino_t inode)1247c478bd9Sstevel@tonic-gate opendir(fileid_t *filep, ino_t inode)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate struct hs_direct hsdep;
1277c478bd9Sstevel@tonic-gate int retval;
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate /* Set up the saio request */
1307c478bd9Sstevel@tonic-gate filep->fi_offset = 0;
1317c478bd9Sstevel@tonic-gate filep->fi_blocknum = hdbtodb(inode);
1327c478bd9Sstevel@tonic-gate filep->fi_count = ISO_SECTOR_SIZE;
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate /* Maybe the block is in the disk block cache */
1357c478bd9Sstevel@tonic-gate if ((filep->fi_memp = get_bcache(filep)) == NULL) {
1367c478bd9Sstevel@tonic-gate /* Not in the block cache so read it from disk */
1377c478bd9Sstevel@tonic-gate if (retval = set_bcache(filep)) {
1387c478bd9Sstevel@tonic-gate return (retval);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate filep->fi_offset = 0;
1437c478bd9Sstevel@tonic-gate filep->fi_blocknum = hdbtodb(inode);
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate if (inode != root_ino)
1467c478bd9Sstevel@tonic-gate return (0);
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate if ((int)(parse_dir(filep, 0, &hsdep)) > 0) {
1497c478bd9Sstevel@tonic-gate hs_seti(filep, &hsdep, inode);
1507c478bd9Sstevel@tonic-gate return (0);
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate return (1);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate static ino_t
find(fileid_t * filep,char * path)1567c478bd9Sstevel@tonic-gate find(fileid_t *filep, char *path)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate register char *q;
1597c478bd9Sstevel@tonic-gate char c;
1607c478bd9Sstevel@tonic-gate ino_t inode;
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate if (path == NULL || *path == '\0') {
1637c478bd9Sstevel@tonic-gate printf("null path\n");
1647c478bd9Sstevel@tonic-gate return (0);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate if ((boothowto & RB_DEBUG) && (boothowto & RB_VERBOSE))
1687c478bd9Sstevel@tonic-gate printf("find(): path=<%s>\n", path);
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate /* Read the ROOT directory */
1717c478bd9Sstevel@tonic-gate if (opendir(filep, inode = root_ino)) {
1727c478bd9Sstevel@tonic-gate printf("find(): root_ino opendir() failed!\n");
1737c478bd9Sstevel@tonic-gate return ((ino_t)-1);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate while (*path) {
1777c478bd9Sstevel@tonic-gate while (*path == '/')
1787c478bd9Sstevel@tonic-gate path++;
1797c478bd9Sstevel@tonic-gate if (*(q = path) == '\0')
1807c478bd9Sstevel@tonic-gate break;
1817c478bd9Sstevel@tonic-gate while (*q != '/' && *q != '\0')
1827c478bd9Sstevel@tonic-gate q++;
1837c478bd9Sstevel@tonic-gate c = *q;
1847c478bd9Sstevel@tonic-gate *q = '\0';
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate if ((inode = dlook(filep, path)) != 0) {
1877c478bd9Sstevel@tonic-gate if (c == '\0')
1887c478bd9Sstevel@tonic-gate break;
1897c478bd9Sstevel@tonic-gate if (opendir(filep, inode)) {
1907c478bd9Sstevel@tonic-gate printf("find(): opendir(%d) failed!\n", inode);
1917c478bd9Sstevel@tonic-gate *q = c;
1927c478bd9Sstevel@tonic-gate return ((ino_t)-1);
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate *q = c;
1957c478bd9Sstevel@tonic-gate path = q;
1967c478bd9Sstevel@tonic-gate continue;
1977c478bd9Sstevel@tonic-gate } else {
1987c478bd9Sstevel@tonic-gate *q = c;
1997c478bd9Sstevel@tonic-gate return (0);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate return (inode);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate static fileid_t *
find_fp(int fd)2067c478bd9Sstevel@tonic-gate find_fp(int fd)
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate fileid_t *filep = head;
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate if (fd >= 0) {
2117c478bd9Sstevel@tonic-gate while ((filep = filep->fi_forw) != head)
2127c478bd9Sstevel@tonic-gate if (fd == filep->fi_filedes)
2137c478bd9Sstevel@tonic-gate return (filep->fi_taken ? filep : 0);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate return (0);
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gate static ino_t
dlook(fileid_t * filep,char * path)2207c478bd9Sstevel@tonic-gate dlook(fileid_t *filep, char *path)
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate int dv = filep->fi_devp->di_dcookie;
2237c478bd9Sstevel@tonic-gate register struct hs_direct *hsdep;
2247c478bd9Sstevel@tonic-gate register struct direct *udp;
2257c478bd9Sstevel@tonic-gate register struct inode *ip;
2267c478bd9Sstevel@tonic-gate struct dirinfo dirp;
2277c478bd9Sstevel@tonic-gate register int len;
2287c478bd9Sstevel@tonic-gate ino_t in;
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate ip = filep->fi_inode;
2317c478bd9Sstevel@tonic-gate if (path == NULL || *path == '\0')
2327c478bd9Sstevel@tonic-gate return (0);
2337c478bd9Sstevel@tonic-gate if ((ip->i_smode & IFMT) != IFDIR) {
2347c478bd9Sstevel@tonic-gate return (0);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate if (ip->i_size == 0) {
2377c478bd9Sstevel@tonic-gate return (0);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate len = strlen(path);
2407c478bd9Sstevel@tonic-gate /* first look through the directory entry cache */
2417c478bd9Sstevel@tonic-gate if (in = get_dcache(dv, path, ip->i_number)) {
2427c478bd9Sstevel@tonic-gate if ((filep->fi_inode = get_icache(dv, in)) != NULL) {
2437c478bd9Sstevel@tonic-gate filep->fi_offset = 0;
2447c478bd9Sstevel@tonic-gate filep->fi_blocknum = hdbtodb(in);
2457c478bd9Sstevel@tonic-gate return (in);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate dirp.loc = 0;
2497c478bd9Sstevel@tonic-gate dirp.fi = filep;
2507c478bd9Sstevel@tonic-gate for (hsdep = readdir(&dirp); hsdep != NULL; hsdep = readdir(&dirp)) {
2517c478bd9Sstevel@tonic-gate udp = &hsdep->hs_ufs_dir;
2527c478bd9Sstevel@tonic-gate if (udp->d_namlen == 1 &&
2537c478bd9Sstevel@tonic-gate udp->d_name[0] == '.' &&
2547c478bd9Sstevel@tonic-gate udp->d_name[1] == '\0')
2557c478bd9Sstevel@tonic-gate continue;
2567c478bd9Sstevel@tonic-gate if (udp->d_namlen == 2 &&
2577c478bd9Sstevel@tonic-gate udp->d_name[0] == '.' &&
2587c478bd9Sstevel@tonic-gate udp->d_name[1] == '.' &&
2597c478bd9Sstevel@tonic-gate udp->d_name[2] == '\0')
2607c478bd9Sstevel@tonic-gate continue;
2617c478bd9Sstevel@tonic-gate if (udp->d_namlen == len && (strcmp(path, udp->d_name) == 0)) {
2627c478bd9Sstevel@tonic-gate set_dcache(dv, path, ip->i_number, udp->d_ino);
2637c478bd9Sstevel@tonic-gate hs_seti(filep, hsdep, udp->d_ino);
2647c478bd9Sstevel@tonic-gate filep->fi_offset = 0;
2657c478bd9Sstevel@tonic-gate filep->fi_blocknum = hdbtodb(udp->d_ino);
2667c478bd9Sstevel@tonic-gate /* put this entry into the cache */
2677c478bd9Sstevel@tonic-gate return (udp->d_ino);
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate /* Allow "*" to print all names at that level, w/out match */
2707c478bd9Sstevel@tonic-gate if (strcmp(path, "*") == 0)
2717c478bd9Sstevel@tonic-gate printf("%s\n", udp->d_name);
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate return (0);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate
2767c478bd9Sstevel@tonic-gate /*
2777c478bd9Sstevel@tonic-gate * get next entry in a directory.
2787c478bd9Sstevel@tonic-gate */
2797c478bd9Sstevel@tonic-gate static struct hs_direct *
readdir(struct dirinfo * dirp)2807c478bd9Sstevel@tonic-gate readdir(struct dirinfo *dirp)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate static struct hs_direct hsdep;
2837c478bd9Sstevel@tonic-gate register struct direct *udp = &hsdep.hs_ufs_dir;
2847c478bd9Sstevel@tonic-gate register struct inode *ip;
2857c478bd9Sstevel@tonic-gate register fileid_t *filep;
2867c478bd9Sstevel@tonic-gate register daddr_t lbn;
2877c478bd9Sstevel@tonic-gate register int off;
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate filep = dirp->fi;
2907c478bd9Sstevel@tonic-gate ip = filep->fi_inode;
2917c478bd9Sstevel@tonic-gate for (;;) {
2927c478bd9Sstevel@tonic-gate if (dirp->loc >= ip->i_size) {
2937c478bd9Sstevel@tonic-gate return (NULL);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate off = dirp->loc & ((1 << ISO_SECTOR_SHIFT) - 1);
2967c478bd9Sstevel@tonic-gate if (off == 0) {
2977c478bd9Sstevel@tonic-gate lbn = hdbtodb(dirp->loc >> ISO_SECTOR_SHIFT);
2987c478bd9Sstevel@tonic-gate filep->fi_blocknum = lbn + hdbtodb(ip->i_number);
2997c478bd9Sstevel@tonic-gate filep->fi_count = ISO_SECTOR_SIZE;
3007c478bd9Sstevel@tonic-gate /* check the block cache */
3017c478bd9Sstevel@tonic-gate if ((filep->fi_memp = get_bcache(filep)) == 0)
3027c478bd9Sstevel@tonic-gate if (set_bcache(filep))
3037c478bd9Sstevel@tonic-gate return ((struct hs_direct *)-1);
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate dirp->loc += parse_dir(filep, off, &hsdep);
3067c478bd9Sstevel@tonic-gate if (udp->d_reclen == 0 && dirp->loc <= ip->i_size) {
3077c478bd9Sstevel@tonic-gate dirp->loc = roundup(dirp->loc, ISO_SECTOR_SIZE);
3087c478bd9Sstevel@tonic-gate continue;
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate return (&hsdep);
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate
3147c478bd9Sstevel@tonic-gate /*
3157c478bd9Sstevel@tonic-gate * Get the next block of data from the file. If possible, dma right into
3167c478bd9Sstevel@tonic-gate * user's buffer
3177c478bd9Sstevel@tonic-gate */
3187c478bd9Sstevel@tonic-gate static int
getblock(fileid_t * filep,caddr_t buf,int count,int * rcount)3197c478bd9Sstevel@tonic-gate getblock(fileid_t *filep, caddr_t buf, int count, int *rcount)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate register struct inode *ip;
3227c478bd9Sstevel@tonic-gate register caddr_t p;
3237c478bd9Sstevel@tonic-gate register int off, size, diff;
3247c478bd9Sstevel@tonic-gate register daddr_t lbn;
3257c478bd9Sstevel@tonic-gate static int pos;
3267c478bd9Sstevel@tonic-gate static char ind[] = "|/-\\"; /* that's entertainment? */
3277c478bd9Sstevel@tonic-gate static int blks_read;
3287c478bd9Sstevel@tonic-gate
3297c478bd9Sstevel@tonic-gate ip = filep->fi_inode;
3307c478bd9Sstevel@tonic-gate p = filep->fi_memp;
3317c478bd9Sstevel@tonic-gate if ((signed)filep->fi_count <= 0) {
3327c478bd9Sstevel@tonic-gate
3337c478bd9Sstevel@tonic-gate /* find the amt left to be read in the file */
3347c478bd9Sstevel@tonic-gate diff = ip->i_size - filep->fi_offset;
3357c478bd9Sstevel@tonic-gate if (diff <= 0) {
3367c478bd9Sstevel@tonic-gate printf("Short read\n");
3377c478bd9Sstevel@tonic-gate return (-1);
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate
3407c478bd9Sstevel@tonic-gate /* which block (or frag) in the file do we read? */
3417c478bd9Sstevel@tonic-gate lbn = hdbtodb(filep->fi_offset >> ISO_SECTOR_SHIFT);
3427c478bd9Sstevel@tonic-gate
3437c478bd9Sstevel@tonic-gate /* which physical block on the device do we read? */
3447c478bd9Sstevel@tonic-gate filep->fi_blocknum = lbn + hdbtodb(ip->i_number);
3457c478bd9Sstevel@tonic-gate
3467c478bd9Sstevel@tonic-gate off = filep->fi_offset & ((1 << ISO_SECTOR_SHIFT) - 1);
3477c478bd9Sstevel@tonic-gate
3487c478bd9Sstevel@tonic-gate size = sizeof (filep->fi_buf);
3497c478bd9Sstevel@tonic-gate if (size > ISO_SECTOR_SIZE)
3507c478bd9Sstevel@tonic-gate size = ISO_SECTOR_SIZE;
3517c478bd9Sstevel@tonic-gate
3527c478bd9Sstevel@tonic-gate filep->fi_count = size;
3537c478bd9Sstevel@tonic-gate filep->fi_memp = filep->fi_buf;
3547c478bd9Sstevel@tonic-gate
3557c478bd9Sstevel@tonic-gate /*
3567c478bd9Sstevel@tonic-gate * optimization if we are reading large blocks of data then
3577c478bd9Sstevel@tonic-gate * we can go directly to user's buffer
3587c478bd9Sstevel@tonic-gate */
3597c478bd9Sstevel@tonic-gate *rcount = 0;
3607c478bd9Sstevel@tonic-gate if (off == 0 && count >= size) {
3617c478bd9Sstevel@tonic-gate filep->fi_memp = buf;
3627c478bd9Sstevel@tonic-gate if (diskread(filep)) {
3637c478bd9Sstevel@tonic-gate return (-1);
3647c478bd9Sstevel@tonic-gate }
3657c478bd9Sstevel@tonic-gate *rcount = size;
3667c478bd9Sstevel@tonic-gate filep->fi_count = 0;
3677c478bd9Sstevel@tonic-gate read_opt++;
3687c478bd9Sstevel@tonic-gate if ((blks_read++ & 0x3) == 0)
3697c478bd9Sstevel@tonic-gate printf("%c\b", ind[pos++ & 3]);
3707c478bd9Sstevel@tonic-gate return (0);
3717c478bd9Sstevel@tonic-gate } else
3727c478bd9Sstevel@tonic-gate if (diskread(filep))
3737c478bd9Sstevel@tonic-gate return (-1);
3747c478bd9Sstevel@tonic-gate
3757c478bd9Sstevel@tonic-gate /*
3767c478bd9Sstevel@tonic-gate * round and round she goes (though not on every block..
3777c478bd9Sstevel@tonic-gate * - OBP's take a fair bit of time to actually print stuff)
3787c478bd9Sstevel@tonic-gate */
3797c478bd9Sstevel@tonic-gate if ((blks_read++ & 0x3) == 0)
3807c478bd9Sstevel@tonic-gate printf("%c\b", ind[pos++ & 3]);
3817c478bd9Sstevel@tonic-gate
3827c478bd9Sstevel@tonic-gate if (filep->fi_offset - off + size >= ip->i_size)
3837c478bd9Sstevel@tonic-gate filep->fi_count = diff + off;
3847c478bd9Sstevel@tonic-gate filep->fi_count -= off;
3857c478bd9Sstevel@tonic-gate p = &filep->fi_memp[off];
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate filep->fi_memp = p;
3887c478bd9Sstevel@tonic-gate return (0);
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate
3917c478bd9Sstevel@tonic-gate
3927c478bd9Sstevel@tonic-gate /*
3937c478bd9Sstevel@tonic-gate * This is the high-level read function. It works like this.
3947c478bd9Sstevel@tonic-gate * We assume that our IO device buffers up some amount of
3957c478bd9Sstevel@tonic-gate * data ant that we can get a ptr to it. Thus we need
3967c478bd9Sstevel@tonic-gate * to actually call the device func about filesize/blocksize times
3977c478bd9Sstevel@tonic-gate * and this greatly increases our IO speed. When we already
3987c478bd9Sstevel@tonic-gate * have data in the buffer, we just return that data (with bcopy() ).
3997c478bd9Sstevel@tonic-gate */
4007c478bd9Sstevel@tonic-gate
4017c478bd9Sstevel@tonic-gate static ssize_t
boot_hsfs_read(int fd,caddr_t buf,size_t count)4027c478bd9Sstevel@tonic-gate boot_hsfs_read(int fd, caddr_t buf, size_t count)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate size_t i, j;
4057c478bd9Sstevel@tonic-gate struct inode *ip;
4067c478bd9Sstevel@tonic-gate caddr_t n;
4077c478bd9Sstevel@tonic-gate fileid_t *filep;
4087c478bd9Sstevel@tonic-gate int rcount;
4097c478bd9Sstevel@tonic-gate
4107c478bd9Sstevel@tonic-gate if (!(filep = find_fp(fd))) {
4117c478bd9Sstevel@tonic-gate return (-1);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate
4147c478bd9Sstevel@tonic-gate ip = filep->fi_inode;
4157c478bd9Sstevel@tonic-gate
4167c478bd9Sstevel@tonic-gate if (filep->fi_offset + count > ip->i_size)
4177c478bd9Sstevel@tonic-gate count = ip->i_size - filep->fi_offset;
4187c478bd9Sstevel@tonic-gate
4197c478bd9Sstevel@tonic-gate /* that was easy */
4207c478bd9Sstevel@tonic-gate if ((i = count) == 0)
4217c478bd9Sstevel@tonic-gate return (0);
4227c478bd9Sstevel@tonic-gate
4237c478bd9Sstevel@tonic-gate n = buf;
4247c478bd9Sstevel@tonic-gate while (i > 0) {
4257c478bd9Sstevel@tonic-gate /* If we need to reload the buffer, do so */
4267c478bd9Sstevel@tonic-gate if ((j = filep->fi_count) == 0) {
4277c478bd9Sstevel@tonic-gate getblock(filep, buf, i, &rcount);
4287c478bd9Sstevel@tonic-gate i -= rcount;
4297c478bd9Sstevel@tonic-gate buf += rcount;
4307c478bd9Sstevel@tonic-gate filep->fi_offset += rcount;
4317c478bd9Sstevel@tonic-gate } else {
4327c478bd9Sstevel@tonic-gate /* else just bcopy from our buffer */
4337c478bd9Sstevel@tonic-gate j = MIN(i, j);
4347c478bd9Sstevel@tonic-gate bcopy(filep->fi_memp, buf, (unsigned)j);
4357c478bd9Sstevel@tonic-gate buf += j;
4367c478bd9Sstevel@tonic-gate filep->fi_memp += j;
4377c478bd9Sstevel@tonic-gate filep->fi_offset += j;
4387c478bd9Sstevel@tonic-gate filep->fi_count -= j;
4397c478bd9Sstevel@tonic-gate i -= j;
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate return (buf - n);
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate
4457c478bd9Sstevel@tonic-gate /*
4467c478bd9Sstevel@tonic-gate * This routine will open a device as it is known by the
4477c478bd9Sstevel@tonic-gate * V2 OBP.
4487c478bd9Sstevel@tonic-gate * Interface Defn:
4497c478bd9Sstevel@tonic-gate * err = mountroot(string);
4507c478bd9Sstevel@tonic-gate * err: 0 on success
4517c478bd9Sstevel@tonic-gate * -1 on failure
4527c478bd9Sstevel@tonic-gate * string: char string describing the properties of the device.
4537c478bd9Sstevel@tonic-gate * We must not dork with any fi[]'s here. Save that for later.
4547c478bd9Sstevel@tonic-gate */
4557c478bd9Sstevel@tonic-gate
4567c478bd9Sstevel@tonic-gate static int
boot_hsfs_mountroot(char * str)4577c478bd9Sstevel@tonic-gate boot_hsfs_mountroot(char *str)
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate ihandle_t h;
4607c478bd9Sstevel@tonic-gate struct hs_volume *fsp;
4617c478bd9Sstevel@tonic-gate char *bufp;
4627c478bd9Sstevel@tonic-gate
4637c478bd9Sstevel@tonic-gate if ((boothowto & RB_DEBUG) && (boothowto & RB_VERBOSE))
4647c478bd9Sstevel@tonic-gate printf("mountroot()\n");
4657c478bd9Sstevel@tonic-gate
4667c478bd9Sstevel@tonic-gate /*
4677c478bd9Sstevel@tonic-gate * If already mounted, just return success.
4687c478bd9Sstevel@tonic-gate */
4697c478bd9Sstevel@tonic-gate if (root_ino != 0) {
4707c478bd9Sstevel@tonic-gate return (0);
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate
4737c478bd9Sstevel@tonic-gate h = prom_open(str);
4747c478bd9Sstevel@tonic-gate
4757c478bd9Sstevel@tonic-gate if (h == 0) {
4767c478bd9Sstevel@tonic-gate printf("Cannot open %s\n", str);
4777c478bd9Sstevel@tonic-gate return (-1);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate
4807c478bd9Sstevel@tonic-gate devp = (devid_t *)bkmem_alloc(sizeof (devid_t));
4817c478bd9Sstevel@tonic-gate devp->di_taken = 1;
4827c478bd9Sstevel@tonic-gate devp->di_dcookie = h;
4837c478bd9Sstevel@tonic-gate devp->di_desc = (char *)bkmem_alloc(strlen(str) + 1);
4847c478bd9Sstevel@tonic-gate (void) strcpy(devp->di_desc, str);
4857c478bd9Sstevel@tonic-gate bzero(devp->un_fs.dummy, sizeof (devp->un_fs.dummy));
4867c478bd9Sstevel@tonic-gate head = (fileid_t *)bkmem_alloc(sizeof (fileid_t));
4877c478bd9Sstevel@tonic-gate head->fi_back = head->fi_forw = head;
4887c478bd9Sstevel@tonic-gate head->fi_filedes = 0;
4897c478bd9Sstevel@tonic-gate head->fi_taken = 0;
4907c478bd9Sstevel@tonic-gate
4917c478bd9Sstevel@tonic-gate /* Setup read of the "superblock" */
4927c478bd9Sstevel@tonic-gate bzero(head->fi_buf, sizeof (head->fi_buf));
4937c478bd9Sstevel@tonic-gate head->fi_devp = devp;
4947c478bd9Sstevel@tonic-gate head->fi_blocknum = hdbtodb(ISO_VOLDESC_SEC);
4957c478bd9Sstevel@tonic-gate head->fi_count = ISO_SECTOR_SIZE;
4967c478bd9Sstevel@tonic-gate head->fi_memp = head->fi_buf;
4977c478bd9Sstevel@tonic-gate head->fi_offset = 0;
4987c478bd9Sstevel@tonic-gate
4997c478bd9Sstevel@tonic-gate if (diskread(head)) {
5007c478bd9Sstevel@tonic-gate printf("mountroot(): read super block failed!\n");
5017c478bd9Sstevel@tonic-gate boot_hsfs_closeall(1);
5027c478bd9Sstevel@tonic-gate return (-1);
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate
5057c478bd9Sstevel@tonic-gate bufp = head->fi_memp;
5067c478bd9Sstevel@tonic-gate fsp = (struct hs_volume *)devp->un_fs.dummy;
5077c478bd9Sstevel@tonic-gate /* Since RRIP is based on ISO9660, that's where we start */
5087c478bd9Sstevel@tonic-gate
5097c478bd9Sstevel@tonic-gate if (ISO_DESC_TYPE(bufp) != ISO_VD_PVD ||
5107c478bd9Sstevel@tonic-gate strncmp((char *)(ISO_std_id(bufp)), (char *)(ISO_ID_STRING),
5117c478bd9Sstevel@tonic-gate ISO_ID_STRLEN) != 0 || ISO_STD_VER(bufp) != ISO_ID_VER) {
5127c478bd9Sstevel@tonic-gate boot_hsfs_closeall(1);
5137c478bd9Sstevel@tonic-gate return (-1);
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate
5167c478bd9Sstevel@tonic-gate /* Now we fill in the volume descriptor */
5177c478bd9Sstevel@tonic-gate fsp->vol_size = ISO_VOL_SIZE(bufp);
5187c478bd9Sstevel@tonic-gate fsp->lbn_size = ISO_BLK_SIZE(bufp);
5197c478bd9Sstevel@tonic-gate fsp->lbn_shift = ISO_SECTOR_SHIFT;
5207c478bd9Sstevel@tonic-gate fsp->lbn_secshift = ISO_SECTOR_SHIFT;
5217c478bd9Sstevel@tonic-gate fsp->vol_set_size = (ushort_t)ISO_SET_SIZE(bufp);
5227c478bd9Sstevel@tonic-gate fsp->vol_set_seq = (ushort_t)ISO_SET_SEQ(bufp);
5237c478bd9Sstevel@tonic-gate
5247c478bd9Sstevel@tonic-gate /* Make sure we have a valid logical block size */
5257c478bd9Sstevel@tonic-gate if (fsp->lbn_size & ~(1 << fsp->lbn_shift)) {
5267c478bd9Sstevel@tonic-gate printf("%d byte logical block size invalid.\n", fsp->lbn_size);
5277c478bd9Sstevel@tonic-gate boot_hsfs_closeall(1);
5287c478bd9Sstevel@tonic-gate return (-1);
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate
5317c478bd9Sstevel@tonic-gate /* Since an HSFS root could be located anywhere on the media! */
5327c478bd9Sstevel@tonic-gate root_ino = IDE_EXT_LBN(ISO_root_dir(bufp));
5337c478bd9Sstevel@tonic-gate
5347c478bd9Sstevel@tonic-gate if ((boothowto & RB_DEBUG) && (boothowto & RB_VERBOSE)) {
5357c478bd9Sstevel@tonic-gate int i;
5367c478bd9Sstevel@tonic-gate
5377c478bd9Sstevel@tonic-gate printf("root_ino=%d\n", root_ino);
5387c478bd9Sstevel@tonic-gate printf("ID=");
5397c478bd9Sstevel@tonic-gate for (i = 0; i < ISO_ID_STRLEN; i++)
5407c478bd9Sstevel@tonic-gate printf("%c", *(ISO_std_id(bufp)+i));
5417c478bd9Sstevel@tonic-gate printf(" VS=%d\n", fsp->vol_size);
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate
5447c478bd9Sstevel@tonic-gate return (0);
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate
5477c478bd9Sstevel@tonic-gate /*
5487c478bd9Sstevel@tonic-gate * Unmount the currently mounted root fs. In practice, this means
5497c478bd9Sstevel@tonic-gate * closing all open files and releasing resources. All of this
5507c478bd9Sstevel@tonic-gate * is done by boot_hsfs_closeall().
5517c478bd9Sstevel@tonic-gate */
5527c478bd9Sstevel@tonic-gate
5537c478bd9Sstevel@tonic-gate int
boot_hsfs_unmountroot(void)5547c478bd9Sstevel@tonic-gate boot_hsfs_unmountroot(void)
5557c478bd9Sstevel@tonic-gate {
5567c478bd9Sstevel@tonic-gate if (root_ino == 0)
5577c478bd9Sstevel@tonic-gate return (-1);
5587c478bd9Sstevel@tonic-gate
5597c478bd9Sstevel@tonic-gate boot_hsfs_closeall(1);
5607c478bd9Sstevel@tonic-gate
5617c478bd9Sstevel@tonic-gate return (0);
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate
5647c478bd9Sstevel@tonic-gate /*
5657c478bd9Sstevel@tonic-gate * We allocate an fd here for use when talking
5667c478bd9Sstevel@tonic-gate * to the file itself.
5677c478bd9Sstevel@tonic-gate */
5687c478bd9Sstevel@tonic-gate
5697c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5707c478bd9Sstevel@tonic-gate static int
boot_hsfs_open(char * filename,int flags)5717c478bd9Sstevel@tonic-gate boot_hsfs_open(char *filename, int flags)
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate fileid_t *filep;
5747c478bd9Sstevel@tonic-gate ino_t inode;
5757c478bd9Sstevel@tonic-gate static int filedes = 1;
5767c478bd9Sstevel@tonic-gate
5777c478bd9Sstevel@tonic-gate /* build and link a new file descriptor */
5787c478bd9Sstevel@tonic-gate filep = (fileid_t *)bkmem_alloc(sizeof (fileid_t));
5797c478bd9Sstevel@tonic-gate filep->fi_back = head->fi_back;
5807c478bd9Sstevel@tonic-gate filep->fi_forw = head;
5817c478bd9Sstevel@tonic-gate head->fi_back->fi_forw = filep;
5827c478bd9Sstevel@tonic-gate head->fi_back = filep;
5837c478bd9Sstevel@tonic-gate
5847c478bd9Sstevel@tonic-gate filep->fi_filedes = filedes++;
5857c478bd9Sstevel@tonic-gate filep->fi_taken = 1;
5867c478bd9Sstevel@tonic-gate filep->fi_path = (char *)bkmem_alloc(strlen(filename) + 1);
5877c478bd9Sstevel@tonic-gate (void) strcpy(filep->fi_path, filename);
5887c478bd9Sstevel@tonic-gate filep->fi_devp = devp; /* dev is already "mounted" */
5897c478bd9Sstevel@tonic-gate
5907c478bd9Sstevel@tonic-gate filep->fi_inode = 0;
5917c478bd9Sstevel@tonic-gate
5927c478bd9Sstevel@tonic-gate inode = find(filep, filename);
5937c478bd9Sstevel@tonic-gate if (inode == (ino_t)0) {
5947c478bd9Sstevel@tonic-gate if ((boothowto & RB_DEBUG) && (boothowto & RB_VERBOSE))
5957c478bd9Sstevel@tonic-gate printf("open(%s) ENOENT\n", filename);
5967c478bd9Sstevel@tonic-gate (void) boot_hsfs_close(filep->fi_filedes);
5977c478bd9Sstevel@tonic-gate return (-1);
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate
6007c478bd9Sstevel@tonic-gate filep->fi_blocknum = hdbtodb(inode);
6017c478bd9Sstevel@tonic-gate filep->fi_offset = filep->fi_count = 0;
6027c478bd9Sstevel@tonic-gate
6037c478bd9Sstevel@tonic-gate if ((boothowto & RB_DEBUG) && (boothowto & RB_VERBOSE))
6047c478bd9Sstevel@tonic-gate printf("open(%s) fd=%d\n", filename, filep->fi_filedes);
6057c478bd9Sstevel@tonic-gate return (filep->fi_filedes);
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate
6087c478bd9Sstevel@tonic-gate /*
6097c478bd9Sstevel@tonic-gate * hsfs_fstat() only supports size, mode and times at present time.
6107c478bd9Sstevel@tonic-gate */
6117c478bd9Sstevel@tonic-gate
6127c478bd9Sstevel@tonic-gate static int
boot_hsfs_fstat(int fd,struct bootstat * stp)6137c478bd9Sstevel@tonic-gate boot_hsfs_fstat(int fd, struct bootstat *stp)
6147c478bd9Sstevel@tonic-gate {
6157c478bd9Sstevel@tonic-gate fileid_t *filep;
6167c478bd9Sstevel@tonic-gate struct inode *ip;
6177c478bd9Sstevel@tonic-gate
6187c478bd9Sstevel@tonic-gate if (!(filep = find_fp(fd)))
6197c478bd9Sstevel@tonic-gate return (-1);
6207c478bd9Sstevel@tonic-gate
6217c478bd9Sstevel@tonic-gate ip = filep->fi_inode;
6227c478bd9Sstevel@tonic-gate
6237c478bd9Sstevel@tonic-gate stp->st_mode = 0;
6247c478bd9Sstevel@tonic-gate stp->st_size = 0;
6257c478bd9Sstevel@tonic-gate
6267c478bd9Sstevel@tonic-gate if (ip == NULL)
6277c478bd9Sstevel@tonic-gate return (0);
6287c478bd9Sstevel@tonic-gate
6297c478bd9Sstevel@tonic-gate switch (ip->i_smode & IFMT) {
6307c478bd9Sstevel@tonic-gate case IFDIR:
6317c478bd9Sstevel@tonic-gate stp->st_mode = S_IFDIR;
6327c478bd9Sstevel@tonic-gate break;
6337c478bd9Sstevel@tonic-gate case IFREG:
6347c478bd9Sstevel@tonic-gate stp->st_mode = S_IFREG;
6357c478bd9Sstevel@tonic-gate break;
6367c478bd9Sstevel@tonic-gate default:
6377c478bd9Sstevel@tonic-gate break;
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate stp->st_size = ip->i_size;
6407c478bd9Sstevel@tonic-gate
6417c478bd9Sstevel@tonic-gate /* file times */
6427c478bd9Sstevel@tonic-gate stp->st_atim.tv_sec = ip->i_atime.tv_sec;
6437c478bd9Sstevel@tonic-gate stp->st_atim.tv_nsec = ip->i_atime.tv_usec * 1000;
6447c478bd9Sstevel@tonic-gate stp->st_mtim.tv_sec = ip->i_mtime.tv_sec;
6457c478bd9Sstevel@tonic-gate stp->st_mtim.tv_nsec = ip->i_mtime.tv_usec * 1000;
6467c478bd9Sstevel@tonic-gate stp->st_ctim.tv_sec = ip->i_ctime.tv_sec;
6477c478bd9Sstevel@tonic-gate stp->st_ctim.tv_nsec = ip->i_ctime.tv_usec * 1000;
6487c478bd9Sstevel@tonic-gate
6497c478bd9Sstevel@tonic-gate return (0);
6507c478bd9Sstevel@tonic-gate }
6517c478bd9Sstevel@tonic-gate
6527c478bd9Sstevel@tonic-gate /*
6537c478bd9Sstevel@tonic-gate * We don't do any IO here.
6547c478bd9Sstevel@tonic-gate * We just play games with the device pointers.
6557c478bd9Sstevel@tonic-gate */
6567c478bd9Sstevel@tonic-gate
6577c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6587c478bd9Sstevel@tonic-gate static off_t
boot_hsfs_lseek(int fd,off_t addr,int whence)6597c478bd9Sstevel@tonic-gate boot_hsfs_lseek(int fd, off_t addr, int whence)
6607c478bd9Sstevel@tonic-gate {
6617c478bd9Sstevel@tonic-gate fileid_t *filep;
6627c478bd9Sstevel@tonic-gate
6637c478bd9Sstevel@tonic-gate if (!(filep = find_fp(fd)))
6647c478bd9Sstevel@tonic-gate return (-1);
6657c478bd9Sstevel@tonic-gate
6667c478bd9Sstevel@tonic-gate filep->fi_offset = addr;
6677c478bd9Sstevel@tonic-gate filep->fi_blocknum = addr / DEV_BSIZE;
6687c478bd9Sstevel@tonic-gate filep->fi_count = 0;
6697c478bd9Sstevel@tonic-gate
6707c478bd9Sstevel@tonic-gate return (0);
6717c478bd9Sstevel@tonic-gate }
6727c478bd9Sstevel@tonic-gate
6737c478bd9Sstevel@tonic-gate static int
boot_hsfs_close(int fd)6747c478bd9Sstevel@tonic-gate boot_hsfs_close(int fd)
6757c478bd9Sstevel@tonic-gate {
6767c478bd9Sstevel@tonic-gate fileid_t *filep;
6777c478bd9Sstevel@tonic-gate
6787c478bd9Sstevel@tonic-gate if ((boothowto & RB_DEBUG) && (boothowto & RB_VERBOSE))
6797c478bd9Sstevel@tonic-gate printf("close(%d)\n", fd);
6807c478bd9Sstevel@tonic-gate
6817c478bd9Sstevel@tonic-gate if (filep = find_fp(fd)) {
6827c478bd9Sstevel@tonic-gate /* Clear the ranks */
6837c478bd9Sstevel@tonic-gate bkmem_free(filep->fi_path, strlen(filep->fi_path)+1);
6847c478bd9Sstevel@tonic-gate filep->fi_blocknum = filep->fi_count = filep->fi_offset = 0;
6857c478bd9Sstevel@tonic-gate filep->fi_memp = (caddr_t)0;
6867c478bd9Sstevel@tonic-gate filep->fi_devp = 0;
6877c478bd9Sstevel@tonic-gate filep->fi_taken = 0;
6887c478bd9Sstevel@tonic-gate
6897c478bd9Sstevel@tonic-gate /* unlink and deallocate node */
6907c478bd9Sstevel@tonic-gate filep->fi_forw->fi_back = filep->fi_back;
6917c478bd9Sstevel@tonic-gate filep->fi_back->fi_forw = filep->fi_forw;
6927c478bd9Sstevel@tonic-gate bkmem_free((char *)filep, sizeof (fileid_t));
6937c478bd9Sstevel@tonic-gate
6947c478bd9Sstevel@tonic-gate return (0);
6957c478bd9Sstevel@tonic-gate } else {
6967c478bd9Sstevel@tonic-gate /* Big problem */
6977c478bd9Sstevel@tonic-gate printf("\nFile descrip %d not allocated!", fd);
6987c478bd9Sstevel@tonic-gate return (-1);
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate
702986fd29aSsetje /* closeall is now idempotent */
7037c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7047c478bd9Sstevel@tonic-gate static void
boot_hsfs_closeall(int flag)7057c478bd9Sstevel@tonic-gate boot_hsfs_closeall(int flag)
7067c478bd9Sstevel@tonic-gate {
7077c478bd9Sstevel@tonic-gate fileid_t *filep = head;
7087c478bd9Sstevel@tonic-gate extern int verbosemode;
7097c478bd9Sstevel@tonic-gate
710986fd29aSsetje if (devp == NULL) {
711986fd29aSsetje if (head)
712986fd29aSsetje prom_panic("boot_hsfs_closeall: head != NULL.\n");
713986fd29aSsetje return;
714986fd29aSsetje }
715986fd29aSsetje
7167c478bd9Sstevel@tonic-gate while ((filep = filep->fi_forw) != head)
7177c478bd9Sstevel@tonic-gate if (filep->fi_taken)
7187c478bd9Sstevel@tonic-gate if (boot_hsfs_close(filep->fi_filedes))
7197c478bd9Sstevel@tonic-gate prom_panic("Filesystem may be inconsistent.\n");
7207c478bd9Sstevel@tonic-gate
721986fd29aSsetje
7227c478bd9Sstevel@tonic-gate release_cache(devp->di_dcookie);
7237c478bd9Sstevel@tonic-gate (void) prom_close(devp->di_dcookie);
7247c478bd9Sstevel@tonic-gate devp->di_taken = 0;
7257c478bd9Sstevel@tonic-gate if (verbosemode)
7267c478bd9Sstevel@tonic-gate print_cache_data();
7277c478bd9Sstevel@tonic-gate bkmem_free((char *)devp, sizeof (devid_t));
7287c478bd9Sstevel@tonic-gate bkmem_free((char *)head, sizeof (fileid_t));
7297c478bd9Sstevel@tonic-gate root_ino = 0;
730986fd29aSsetje devp = NULL;
731986fd29aSsetje head = NULL;
7327c478bd9Sstevel@tonic-gate }
7337c478bd9Sstevel@tonic-gate
7347c478bd9Sstevel@tonic-gate static uint_t
parse_dir(fileid_t * filep,int offset,struct hs_direct * hsdep)7357c478bd9Sstevel@tonic-gate parse_dir(fileid_t *filep, int offset, struct hs_direct *hsdep)
7367c478bd9Sstevel@tonic-gate {
7377c478bd9Sstevel@tonic-gate char *bufp = (char *)(filep->fi_memp + offset);
7387c478bd9Sstevel@tonic-gate struct direct *udp = &hsdep->hs_ufs_dir;
7397c478bd9Sstevel@tonic-gate struct hs_direntry *hdp = &hsdep->hs_dir;
7407c478bd9Sstevel@tonic-gate uint_t ce_lbn;
7417c478bd9Sstevel@tonic-gate uint_t ce_len;
7427c478bd9Sstevel@tonic-gate uint_t nmlen;
7437c478bd9Sstevel@tonic-gate uint_t i;
7447c478bd9Sstevel@tonic-gate uchar_t c;
7457c478bd9Sstevel@tonic-gate int ret_code = 0;
7467c478bd9Sstevel@tonic-gate
7477c478bd9Sstevel@tonic-gate if ((udp->d_reclen = IDE_DIR_LEN(bufp)) == 0)
7487c478bd9Sstevel@tonic-gate return (0);
7497c478bd9Sstevel@tonic-gate
7507c478bd9Sstevel@tonic-gate hdp->ext_lbn = IDE_EXT_LBN(bufp);
7517c478bd9Sstevel@tonic-gate hdp->ext_size = IDE_EXT_SIZE(bufp);
7527c478bd9Sstevel@tonic-gate hs_dodates(HS_VOL_TYPE_ISO, hdp, bufp);
7537c478bd9Sstevel@tonic-gate hdp->xar_len = IDE_XAR_LEN(bufp);
7547c478bd9Sstevel@tonic-gate hdp->intlf_sz = IDE_INTRLV_SIZE(bufp);
7557c478bd9Sstevel@tonic-gate hdp->intlf_sk = IDE_INTRLV_SKIP(bufp);
7567c478bd9Sstevel@tonic-gate hdp->sym_link = NULL;
7577c478bd9Sstevel@tonic-gate
7587c478bd9Sstevel@tonic-gate udp->d_ino = hdp->ext_lbn;
7597c478bd9Sstevel@tonic-gate
7607c478bd9Sstevel@tonic-gate c = IDE_FLAGS(bufp);
7617c478bd9Sstevel@tonic-gate if (IDE_REGULAR_FILE(c)) {
7627c478bd9Sstevel@tonic-gate hdp->type = VREG;
7637c478bd9Sstevel@tonic-gate hdp->mode = IFREG;
7647c478bd9Sstevel@tonic-gate hdp->nlink = 1;
7657c478bd9Sstevel@tonic-gate } else if (IDE_REGULAR_DIR(c)) {
7667c478bd9Sstevel@tonic-gate hdp->type = VDIR;
7677c478bd9Sstevel@tonic-gate hdp->mode = IFDIR;
7687c478bd9Sstevel@tonic-gate hdp->nlink = 2;
7697c478bd9Sstevel@tonic-gate } else {
7707c478bd9Sstevel@tonic-gate printf("parse_dir(): file type=0x%x unknown.\n", c);
7717c478bd9Sstevel@tonic-gate return ((uint_t)-1);
7727c478bd9Sstevel@tonic-gate }
7737c478bd9Sstevel@tonic-gate
7747c478bd9Sstevel@tonic-gate /* Some initial conditions */
7757c478bd9Sstevel@tonic-gate nmlen = IDE_NAME_LEN(bufp);
7767c478bd9Sstevel@tonic-gate c = *IDE_NAME(bufp);
7777c478bd9Sstevel@tonic-gate /* Special Case: Current Directory */
7787c478bd9Sstevel@tonic-gate if (nmlen == 1 && c == '\0') {
7797c478bd9Sstevel@tonic-gate udp->d_name[0] = '.';
7807c478bd9Sstevel@tonic-gate udp->d_name[1] = '\0';
7817c478bd9Sstevel@tonic-gate udp->d_namlen = 1;
7827c478bd9Sstevel@tonic-gate /* Special Case: Parent Directory */
7837c478bd9Sstevel@tonic-gate } else if (nmlen == 1 && c == '\001') {
7847c478bd9Sstevel@tonic-gate udp->d_name[0] = '.';
7857c478bd9Sstevel@tonic-gate udp->d_name[1] = '.';
7867c478bd9Sstevel@tonic-gate udp->d_name[2] = '\0';
7877c478bd9Sstevel@tonic-gate udp->d_namlen = 2;
7887c478bd9Sstevel@tonic-gate /* Other file name */
7897c478bd9Sstevel@tonic-gate } else {
7907c478bd9Sstevel@tonic-gate udp->d_namlen = 0;
7917c478bd9Sstevel@tonic-gate for (i = 0; i < nmlen; i++) {
7927c478bd9Sstevel@tonic-gate c = *(IDE_name(bufp)+i);
7937c478bd9Sstevel@tonic-gate if (c == ';')
7947c478bd9Sstevel@tonic-gate break;
7957c478bd9Sstevel@tonic-gate else if (c == ' ')
7967c478bd9Sstevel@tonic-gate continue;
7977c478bd9Sstevel@tonic-gate else
7987c478bd9Sstevel@tonic-gate udp->d_name[udp->d_namlen++] = c;
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate udp->d_name[udp->d_namlen] = '\0';
8017c478bd9Sstevel@tonic-gate }
8027c478bd9Sstevel@tonic-gate /* System Use Fields */
8037c478bd9Sstevel@tonic-gate ce_len = IDE_SUA_LEN(bufp);
8047c478bd9Sstevel@tonic-gate ce_lbn = 0;
8057c478bd9Sstevel@tonic-gate if ((int)(ce_len) > 0) {
806986fd29aSsetje ce_lbn = parse_susp((char *)IDE_sys_use_area(bufp),
807986fd29aSsetje &ce_len, hsdep);
8087c478bd9Sstevel@tonic-gate while (ce_lbn) {
8097c478bd9Sstevel@tonic-gate daddr_t save_blocknum = filep->fi_blocknum;
8107c478bd9Sstevel@tonic-gate daddr_t save_offset = filep->fi_offset;
8117c478bd9Sstevel@tonic-gate caddr_t save_memp = filep->fi_memp;
8127c478bd9Sstevel@tonic-gate uint_t save_count = filep->fi_count;
8137c478bd9Sstevel@tonic-gate
8147c478bd9Sstevel@tonic-gate #ifdef noisy
8157c478bd9Sstevel@tonic-gate print_io_req(filep, "parse_dir(): [I]");
8167c478bd9Sstevel@tonic-gate #endif /* noisy */
8177c478bd9Sstevel@tonic-gate
8187c478bd9Sstevel@tonic-gate filep->fi_blocknum = hdbtodb(ce_lbn);
8197c478bd9Sstevel@tonic-gate filep->fi_offset = 0;
8207c478bd9Sstevel@tonic-gate filep->fi_count = ISO_SECTOR_SIZE;
8217c478bd9Sstevel@tonic-gate
8227c478bd9Sstevel@tonic-gate #ifdef noisy
8237c478bd9Sstevel@tonic-gate print_io_req(filep, "parse_dir(): [0]");
8247c478bd9Sstevel@tonic-gate #endif /* noisy */
8257c478bd9Sstevel@tonic-gate
8267c478bd9Sstevel@tonic-gate if ((filep->fi_memp = get_bcache(filep)) == 0)
8277c478bd9Sstevel@tonic-gate ret_code = set_bcache(filep);
8287c478bd9Sstevel@tonic-gate
8297c478bd9Sstevel@tonic-gate #ifdef noisy
8307c478bd9Sstevel@tonic-gate print_io_req(filep, "parse_dir(): [1]");
8317c478bd9Sstevel@tonic-gate #endif /* noisy */
8327c478bd9Sstevel@tonic-gate
8337c478bd9Sstevel@tonic-gate if (ret_code) {
8347c478bd9Sstevel@tonic-gate filep->fi_blocknum = save_blocknum;
8357c478bd9Sstevel@tonic-gate filep->fi_offset = save_offset;
8367c478bd9Sstevel@tonic-gate filep->fi_memp = save_memp;
8377c478bd9Sstevel@tonic-gate filep->fi_count = save_count;
838986fd29aSsetje printf("parse_dir(): "
839986fd29aSsetje "set_bcache() failed (%d)\n", ret_code);
8407c478bd9Sstevel@tonic-gate break;
8417c478bd9Sstevel@tonic-gate }
8427c478bd9Sstevel@tonic-gate ce_lbn = parse_susp(filep->fi_memp, &ce_len, hsdep);
8437c478bd9Sstevel@tonic-gate
8447c478bd9Sstevel@tonic-gate filep->fi_blocknum = save_blocknum;
8457c478bd9Sstevel@tonic-gate filep->fi_offset = save_offset;
8467c478bd9Sstevel@tonic-gate filep->fi_memp = save_memp;
8477c478bd9Sstevel@tonic-gate filep->fi_count = save_count;
8487c478bd9Sstevel@tonic-gate
8497c478bd9Sstevel@tonic-gate #ifdef noisy
8507c478bd9Sstevel@tonic-gate print_io_req(filep, "parse_dir(): [2]");
8517c478bd9Sstevel@tonic-gate #endif /* noisy */
8527c478bd9Sstevel@tonic-gate }
8537c478bd9Sstevel@tonic-gate }
8547c478bd9Sstevel@tonic-gate
8557c478bd9Sstevel@tonic-gate return (udp->d_reclen);
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate
8587c478bd9Sstevel@tonic-gate static uint_t
parse_susp(char * bufp,uint_t * ce_len,struct hs_direct * hsdep)8597c478bd9Sstevel@tonic-gate parse_susp(char *bufp, uint_t *ce_len, struct hs_direct *hsdep)
8607c478bd9Sstevel@tonic-gate {
8617c478bd9Sstevel@tonic-gate struct direct *udp = &hsdep->hs_ufs_dir;
8627c478bd9Sstevel@tonic-gate uchar_t *susp;
8637c478bd9Sstevel@tonic-gate uint_t cur_off = 0;
8647c478bd9Sstevel@tonic-gate uint_t blk_len = *ce_len;
8657c478bd9Sstevel@tonic-gate uint_t susp_len = 0;
8667c478bd9Sstevel@tonic-gate uint_t ce_lbn = 0;
8677c478bd9Sstevel@tonic-gate uint_t i;
8687c478bd9Sstevel@tonic-gate
8697c478bd9Sstevel@tonic-gate while (cur_off < blk_len) {
8707c478bd9Sstevel@tonic-gate susp = (uchar_t *)(bufp + cur_off);
8717c478bd9Sstevel@tonic-gate if (susp[0] == '\0' || susp[1] == '\0')
8727c478bd9Sstevel@tonic-gate break;
8737c478bd9Sstevel@tonic-gate susp_len = SUF_LEN(susp);
8747c478bd9Sstevel@tonic-gate if (susp_len == 0)
8757c478bd9Sstevel@tonic-gate break;
8767c478bd9Sstevel@tonic-gate for (i = 0; i < hsfs_num_sig; i++) {
877986fd29aSsetje if (strncmp(hsfs_sig_tab[i],
878986fd29aSsetje (char *)susp, SUF_SIG_LEN) == 0) {
8797c478bd9Sstevel@tonic-gate #ifdef noisy
880986fd29aSsetje if ((boothowto & RB_DEBUG) &&
881986fd29aSsetje (boothowto & RB_VERBOSE))
882986fd29aSsetje printf(" SUSP_%c%c %d\n",
883986fd29aSsetje susp[0], susp[1], susp_len);
8847c478bd9Sstevel@tonic-gate #endif /* noisy */
8857c478bd9Sstevel@tonic-gate switch (i) {
8867c478bd9Sstevel@tonic-gate case SUSP_SP_IX:
8877c478bd9Sstevel@tonic-gate if (CHECK_BYTES_OK(susp)) {
888986fd29aSsetje sua_offset =
889986fd29aSsetje SP_SUA_OFFSET(susp);
8907c478bd9Sstevel@tonic-gate #ifdef lint
891986fd29aSsetje /* this may not be needed */
8927c478bd9Sstevel@tonic-gate i = (int)sua_offset;
8937c478bd9Sstevel@tonic-gate #endif /* lint */
8947c478bd9Sstevel@tonic-gate }
8957c478bd9Sstevel@tonic-gate break;
8967c478bd9Sstevel@tonic-gate
8977c478bd9Sstevel@tonic-gate case SUSP_CE_IX:
8987c478bd9Sstevel@tonic-gate ce_lbn = CE_BLK_LOC(susp);
8997c478bd9Sstevel@tonic-gate *ce_len = CE_CONT_LEN(susp);
9007c478bd9Sstevel@tonic-gate #ifdef noisy
9017c478bd9Sstevel@tonic-gate if ((boothowto & RB_DEBUG) &&
9027c478bd9Sstevel@tonic-gate (boothowto & RB_VERBOSE))
9037c478bd9Sstevel@tonic-gate printf("parse_susp(): "
904986fd29aSsetje "CE: ce_lbn = %d "
905986fd29aSsetje "ce_len=%d\n",
9067c478bd9Sstevel@tonic-gate ce_lbn, *ce_len);
9077c478bd9Sstevel@tonic-gate #endif /* noisy */
9087c478bd9Sstevel@tonic-gate break;
9097c478bd9Sstevel@tonic-gate
9107c478bd9Sstevel@tonic-gate case SUSP_ST_IX:
911986fd29aSsetje printf("parse_susp(): ST: returning "
912986fd29aSsetje "%d\n", ce_lbn);
9137c478bd9Sstevel@tonic-gate return (ce_lbn);
9147c478bd9Sstevel@tonic-gate
9157c478bd9Sstevel@tonic-gate case RRIP_SL_IX:
9167c478bd9Sstevel@tonic-gate #ifdef noisy
9177c478bd9Sstevel@tonic-gate if ((boothowto & RB_DEBUG) &&
9187c478bd9Sstevel@tonic-gate (boothowto & RB_VERBOSE))
919986fd29aSsetje printf("parse_susp(): "
920986fd29aSsetje "******* SL *******\n");
9217c478bd9Sstevel@tonic-gate #endif /* noisy */
9227c478bd9Sstevel@tonic-gate break;
9237c478bd9Sstevel@tonic-gate
9247c478bd9Sstevel@tonic-gate case RRIP_RR_IX:
9257c478bd9Sstevel@tonic-gate break;
9267c478bd9Sstevel@tonic-gate
9277c478bd9Sstevel@tonic-gate case RRIP_NM_IX:
9287c478bd9Sstevel@tonic-gate if (!RRIP_NAME_FLAGS(susp)) {
929986fd29aSsetje udp->d_namlen =
930986fd29aSsetje RRIP_NAME_LEN(susp);
9317c478bd9Sstevel@tonic-gate bcopy((char *)RRIP_name(susp),
9327c478bd9Sstevel@tonic-gate (char *)udp->d_name,
9337c478bd9Sstevel@tonic-gate udp->d_namlen);
934986fd29aSsetje udp->d_name
935986fd29aSsetje [udp->d_namlen] = '\0';
9367c478bd9Sstevel@tonic-gate }
9377c478bd9Sstevel@tonic-gate break;
9387c478bd9Sstevel@tonic-gate }
9397c478bd9Sstevel@tonic-gate cur_off += susp_len;
9407c478bd9Sstevel@tonic-gate break;
9417c478bd9Sstevel@tonic-gate }
9427c478bd9Sstevel@tonic-gate }
9437c478bd9Sstevel@tonic-gate if (i > hsfs_num_sig) {
9447c478bd9Sstevel@tonic-gate printf("parse_susp(): Bad SUSP\n");
9457c478bd9Sstevel@tonic-gate cur_off = blk_len;
9467c478bd9Sstevel@tonic-gate break;
9477c478bd9Sstevel@tonic-gate }
9487c478bd9Sstevel@tonic-gate }
9497c478bd9Sstevel@tonic-gate return (ce_lbn);
9507c478bd9Sstevel@tonic-gate }
9517c478bd9Sstevel@tonic-gate
9527c478bd9Sstevel@tonic-gate static void
hs_seti(fileid_t * filep,struct hs_direct * hsdep,ino_t inode)9537c478bd9Sstevel@tonic-gate hs_seti(fileid_t *filep, struct hs_direct *hsdep, ino_t inode)
9547c478bd9Sstevel@tonic-gate {
9557c478bd9Sstevel@tonic-gate register struct inode *ip;
9567c478bd9Sstevel@tonic-gate int dv = filep->fi_devp->di_dcookie;
9577c478bd9Sstevel@tonic-gate
9587c478bd9Sstevel@tonic-gate /* Try the inode cache first */
9597c478bd9Sstevel@tonic-gate if ((filep->fi_inode = get_icache(dv, inode)) != NULL)
9607c478bd9Sstevel@tonic-gate return;
9617c478bd9Sstevel@tonic-gate
9627c478bd9Sstevel@tonic-gate filep->fi_inode = (struct inode *)bkmem_alloc(sizeof (struct inode));
9637c478bd9Sstevel@tonic-gate ip = filep->fi_inode;
9647c478bd9Sstevel@tonic-gate bzero((char *)ip, sizeof (struct inode));
9657c478bd9Sstevel@tonic-gate ip->i_size = hsdep->hs_dir.ext_size;
9667c478bd9Sstevel@tonic-gate ip->i_smode = hsdep->hs_dir.mode;
9677c478bd9Sstevel@tonic-gate ip->i_number = inode;
9687c478bd9Sstevel@tonic-gate ip->i_atime.tv_sec = hsdep->hs_dir.adate.tv_sec;
9697c478bd9Sstevel@tonic-gate ip->i_atime.tv_usec = hsdep->hs_dir.adate.tv_usec;
9707c478bd9Sstevel@tonic-gate ip->i_ctime.tv_sec = hsdep->hs_dir.cdate.tv_sec;
9717c478bd9Sstevel@tonic-gate ip->i_ctime.tv_usec = hsdep->hs_dir.cdate.tv_usec;
9727c478bd9Sstevel@tonic-gate ip->i_mtime.tv_sec = hsdep->hs_dir.mdate.tv_sec;
9737c478bd9Sstevel@tonic-gate ip->i_mtime.tv_usec = hsdep->hs_dir.mdate.tv_usec;
9747c478bd9Sstevel@tonic-gate set_icache(dv, inode, ip, sizeof (struct inode));
9757c478bd9Sstevel@tonic-gate }
9767c478bd9Sstevel@tonic-gate
9777c478bd9Sstevel@tonic-gate #ifdef noisy
9787c478bd9Sstevel@tonic-gate static void
print_io_req(fileid_t * filep,char * str)9797c478bd9Sstevel@tonic-gate print_io_req(fileid_t *filep, char *str)
9807c478bd9Sstevel@tonic-gate {
9817c478bd9Sstevel@tonic-gate printf("%s o=%d b=%d c=%d m=%x\n",
9827c478bd9Sstevel@tonic-gate str,
9837c478bd9Sstevel@tonic-gate filep->fi_offset,
9847c478bd9Sstevel@tonic-gate filep->fi_blocknum,
9857c478bd9Sstevel@tonic-gate filep->fi_count,
9867c478bd9Sstevel@tonic-gate (uint_t)filep->fi_memp);
9877c478bd9Sstevel@tonic-gate }
9887c478bd9Sstevel@tonic-gate #endif /* noisy */
9897c478bd9Sstevel@tonic-gate
9907c478bd9Sstevel@tonic-gate static int
boot_hsfs_getdents(int fd,struct dirent * dep,unsigned size)9917c478bd9Sstevel@tonic-gate boot_hsfs_getdents(int fd, struct dirent *dep, unsigned size)
9927c478bd9Sstevel@tonic-gate {
9937c478bd9Sstevel@tonic-gate /*
9947c478bd9Sstevel@tonic-gate * Read directory entries from the file open on "fd" into the
9957c478bd9Sstevel@tonic-gate * "size"-byte buffer at "dep" until the buffer is exhausted
9967c478bd9Sstevel@tonic-gate * or we reach EOF on the directory. Returns the number of
9977c478bd9Sstevel@tonic-gate * entries read.
9987c478bd9Sstevel@tonic-gate */
9997c478bd9Sstevel@tonic-gate int n;
10007c478bd9Sstevel@tonic-gate int cnt = 0;
10017c478bd9Sstevel@tonic-gate struct dirinfo dir;
10027c478bd9Sstevel@tonic-gate struct hs_direct *hdp;
10037c478bd9Sstevel@tonic-gate unsigned long oldoff, oldblok;
10047c478bd9Sstevel@tonic-gate
1005*e0731422SRichard Lowe #define SLOP (sizeof (struct dirent) - offsetof(struct dirent, d_name[1]))
10067c478bd9Sstevel@tonic-gate
10077c478bd9Sstevel@tonic-gate if (!(dir.fi = find_fp(fd)) ||
10087c478bd9Sstevel@tonic-gate ((dir.fi->fi_inode->i_smode & IFMT) != IFDIR)) {
10097c478bd9Sstevel@tonic-gate /*
10107c478bd9Sstevel@tonic-gate * Bogus file descriptor, bail out now!
10117c478bd9Sstevel@tonic-gate */
10127c478bd9Sstevel@tonic-gate return (-1);
10137c478bd9Sstevel@tonic-gate }
10147c478bd9Sstevel@tonic-gate
10157c478bd9Sstevel@tonic-gate oldoff = dir.loc = dir.fi->fi_offset;
10167c478bd9Sstevel@tonic-gate oldblok = dir.fi->fi_blocknum;
10177c478bd9Sstevel@tonic-gate
10187c478bd9Sstevel@tonic-gate for (hdp = readdir(&dir); hdp; hdp = readdir(&dir)) {
10197c478bd9Sstevel@tonic-gate /*
10207c478bd9Sstevel@tonic-gate * Compute name length and break loop if there's not
10217c478bd9Sstevel@tonic-gate * enough space in the output buffer for the next
10227c478bd9Sstevel@tonic-gate * entry.
10237c478bd9Sstevel@tonic-gate *
10247c478bd9Sstevel@tonic-gate * NOTE: "SLOP" is the number of bytes inserted into the dirent
10257c478bd9Sstevel@tonic-gate * struct's "d_name" field by the compiler to preserve
10267c478bd9Sstevel@tonic-gate * alignment.
10277c478bd9Sstevel@tonic-gate */
10287c478bd9Sstevel@tonic-gate n = strlen(hdp->hs_ufs_dir.d_name);
10297c478bd9Sstevel@tonic-gate n = roundup((sizeof (struct dirent) + ((n > SLOP) ? n : 0)),
10307c478bd9Sstevel@tonic-gate sizeof (off_t));
10317c478bd9Sstevel@tonic-gate
10327c478bd9Sstevel@tonic-gate if (n > size) {
10337c478bd9Sstevel@tonic-gate dir.fi->fi_blocknum = oldblok;
10347c478bd9Sstevel@tonic-gate dir.fi->fi_offset = oldoff;
10357c478bd9Sstevel@tonic-gate break;
10367c478bd9Sstevel@tonic-gate }
10377c478bd9Sstevel@tonic-gate
10387c478bd9Sstevel@tonic-gate oldblok = dir.fi->fi_blocknum;
10397c478bd9Sstevel@tonic-gate oldoff = dir.loc;
10407c478bd9Sstevel@tonic-gate size -= n;
10417c478bd9Sstevel@tonic-gate cnt += 1;
10427c478bd9Sstevel@tonic-gate
10437c478bd9Sstevel@tonic-gate (void) strcpy(dep->d_name, hdp->hs_ufs_dir.d_name);
10447c478bd9Sstevel@tonic-gate dep->d_ino = hdp->hs_ufs_dir.d_ino;
10457c478bd9Sstevel@tonic-gate dep->d_off = dir.loc;
10467c478bd9Sstevel@tonic-gate dep->d_reclen = (unsigned short)n;
10477c478bd9Sstevel@tonic-gate
10487c478bd9Sstevel@tonic-gate dep = (struct dirent *)((char *)dep + n);
10497c478bd9Sstevel@tonic-gate }
10507c478bd9Sstevel@tonic-gate
10517c478bd9Sstevel@tonic-gate #undef SLOP
10527c478bd9Sstevel@tonic-gate
10537c478bd9Sstevel@tonic-gate return (cnt);
10547c478bd9Sstevel@tonic-gate }
10557c478bd9Sstevel@tonic-gate
10567c478bd9Sstevel@tonic-gate static void
hs_dodates(enum hs_vol_type type,struct hs_direntry * hdp,char * bufp)10577c478bd9Sstevel@tonic-gate hs_dodates(enum hs_vol_type type, struct hs_direntry *hdp, char *bufp)
10587c478bd9Sstevel@tonic-gate {
10597c478bd9Sstevel@tonic-gate if (type == HS_VOL_TYPE_HS) {
10607c478bd9Sstevel@tonic-gate hs_parse_dirdate(HDE_cdate(bufp), &hdp->cdate);
10617c478bd9Sstevel@tonic-gate hs_parse_dirdate(HDE_cdate(bufp), &hdp->adate);
10627c478bd9Sstevel@tonic-gate hs_parse_dirdate(HDE_cdate(bufp), &hdp->mdate);
10637c478bd9Sstevel@tonic-gate } else if (type == HS_VOL_TYPE_ISO) {
10647c478bd9Sstevel@tonic-gate hs_parse_dirdate(IDE_cdate(bufp), &hdp->cdate);
10657c478bd9Sstevel@tonic-gate hs_parse_dirdate(IDE_cdate(bufp), &hdp->adate);
10667c478bd9Sstevel@tonic-gate hs_parse_dirdate(IDE_cdate(bufp), &hdp->mdate);
10677c478bd9Sstevel@tonic-gate } else
10687c478bd9Sstevel@tonic-gate prom_panic("hs_dodates: bad volume type");
10697c478bd9Sstevel@tonic-gate }
10707c478bd9Sstevel@tonic-gate
10717c478bd9Sstevel@tonic-gate /*
10727c478bd9Sstevel@tonic-gate * hs_parse_dirdate
10737c478bd9Sstevel@tonic-gate *
10747c478bd9Sstevel@tonic-gate * Parse the short 'directory-format' date into a Unix timeval.
10757c478bd9Sstevel@tonic-gate * This is the date format used in Directory Entries.
10767c478bd9Sstevel@tonic-gate *
10777c478bd9Sstevel@tonic-gate * If the date is not representable, make something up.
10787c478bd9Sstevel@tonic-gate */
10797c478bd9Sstevel@tonic-gate void
hs_parse_dirdate(uchar_t * dp,struct timeval * tvp)10807c478bd9Sstevel@tonic-gate hs_parse_dirdate(uchar_t *dp, struct timeval *tvp)
10817c478bd9Sstevel@tonic-gate {
10827c478bd9Sstevel@tonic-gate int year, month, day, hour, minute, sec, gmtoff;
10837c478bd9Sstevel@tonic-gate
10847c478bd9Sstevel@tonic-gate year = HDE_DATE_YEAR(dp);
10857c478bd9Sstevel@tonic-gate month = HDE_DATE_MONTH(dp);
10867c478bd9Sstevel@tonic-gate day = HDE_DATE_DAY(dp);
10877c478bd9Sstevel@tonic-gate hour = HDE_DATE_HOUR(dp);
10887c478bd9Sstevel@tonic-gate minute = HDE_DATE_MIN(dp);
10897c478bd9Sstevel@tonic-gate sec = HDE_DATE_SEC(dp);
10907c478bd9Sstevel@tonic-gate gmtoff = HDE_DATE_GMTOFF(dp);
10917c478bd9Sstevel@tonic-gate
10927c478bd9Sstevel@tonic-gate tvp->tv_usec = 0;
10937c478bd9Sstevel@tonic-gate if (year < THE_EPOCH) {
10947c478bd9Sstevel@tonic-gate tvp->tv_sec = 0;
10957c478bd9Sstevel@tonic-gate } else {
10967c478bd9Sstevel@tonic-gate tvp->tv_sec = hs_date_to_gmtime(year, month, day, gmtoff);
10977c478bd9Sstevel@tonic-gate if (tvp->tv_sec != -1) {
10987c478bd9Sstevel@tonic-gate tvp->tv_sec += ((hour * 60) + minute) * 60 + sec;
10997c478bd9Sstevel@tonic-gate }
11007c478bd9Sstevel@tonic-gate }
11017c478bd9Sstevel@tonic-gate
11027c478bd9Sstevel@tonic-gate return;
11037c478bd9Sstevel@tonic-gate
11047c478bd9Sstevel@tonic-gate }
11057c478bd9Sstevel@tonic-gate
11067c478bd9Sstevel@tonic-gate /*
11077c478bd9Sstevel@tonic-gate * hs_parse_longdate
11087c478bd9Sstevel@tonic-gate *
11097c478bd9Sstevel@tonic-gate * Parse the long 'user-oriented' date into a Unix timeval.
11107c478bd9Sstevel@tonic-gate * This is the date format used in the Volume Descriptor.
11117c478bd9Sstevel@tonic-gate *
11127c478bd9Sstevel@tonic-gate * If the date is not representable, make something up.
11137c478bd9Sstevel@tonic-gate */
11147c478bd9Sstevel@tonic-gate void
hs_parse_longdate(uchar_t * dp,struct timeval * tvp)11157c478bd9Sstevel@tonic-gate hs_parse_longdate(uchar_t *dp, struct timeval *tvp)
11167c478bd9Sstevel@tonic-gate {
11177c478bd9Sstevel@tonic-gate int year, month, day, hour, minute, sec, gmtoff;
11187c478bd9Sstevel@tonic-gate
11197c478bd9Sstevel@tonic-gate year = HSV_DATE_YEAR(dp);
11207c478bd9Sstevel@tonic-gate month = HSV_DATE_MONTH(dp);
11217c478bd9Sstevel@tonic-gate day = HSV_DATE_DAY(dp);
11227c478bd9Sstevel@tonic-gate hour = HSV_DATE_HOUR(dp);
11237c478bd9Sstevel@tonic-gate minute = HSV_DATE_MIN(dp);
11247c478bd9Sstevel@tonic-gate sec = HSV_DATE_SEC(dp);
11257c478bd9Sstevel@tonic-gate gmtoff = HSV_DATE_GMTOFF(dp);
11267c478bd9Sstevel@tonic-gate
11277c478bd9Sstevel@tonic-gate tvp->tv_usec = 0;
11287c478bd9Sstevel@tonic-gate if (year < THE_EPOCH) {
11297c478bd9Sstevel@tonic-gate tvp->tv_sec = 0;
11307c478bd9Sstevel@tonic-gate } else {
11317c478bd9Sstevel@tonic-gate tvp->tv_sec = hs_date_to_gmtime(year, month, day, gmtoff);
11327c478bd9Sstevel@tonic-gate if (tvp->tv_sec != -1) {
11337c478bd9Sstevel@tonic-gate tvp->tv_sec += ((hour * 60) + minute) * 60 + sec;
11347c478bd9Sstevel@tonic-gate tvp->tv_usec = HSV_DATE_HSEC(dp) * 10000;
11357c478bd9Sstevel@tonic-gate }
11367c478bd9Sstevel@tonic-gate }
11377c478bd9Sstevel@tonic-gate
11387c478bd9Sstevel@tonic-gate }
11397c478bd9Sstevel@tonic-gate
11407c478bd9Sstevel@tonic-gate /* cumulative number of seconds per month, non-leap and leap-year versions */
11417c478bd9Sstevel@tonic-gate static time_t cum_sec[] = {
11427c478bd9Sstevel@tonic-gate 0x0, 0x28de80, 0x4dc880, 0x76a700, 0x9e3400, 0xc71280,
11437c478bd9Sstevel@tonic-gate 0xee9f80, 0x1177e00, 0x1405c80, 0x167e980, 0x190c800, 0x1b85500
11447c478bd9Sstevel@tonic-gate };
11457c478bd9Sstevel@tonic-gate static time_t cum_sec_leap[] = {
11467c478bd9Sstevel@tonic-gate 0x0, 0x28de80, 0x4f1a00, 0x77f880, 0x9f8580, 0xc86400,
11477c478bd9Sstevel@tonic-gate 0xeff100, 0x118cf80, 0x141ae00, 0x1693b00, 0x1921980, 0x1b9a680
11487c478bd9Sstevel@tonic-gate };
11497c478bd9Sstevel@tonic-gate #define SEC_PER_DAY 0x15180
11507c478bd9Sstevel@tonic-gate #define SEC_PER_YEAR 0x1e13380
11517c478bd9Sstevel@tonic-gate
11527c478bd9Sstevel@tonic-gate /*
11537c478bd9Sstevel@tonic-gate * hs_date_to_gmtime
11547c478bd9Sstevel@tonic-gate *
11557c478bd9Sstevel@tonic-gate * Convert year(1970-2099)/month(1-12)/day(1-31) to seconds-since-1970/1/1.
11567c478bd9Sstevel@tonic-gate *
11577c478bd9Sstevel@tonic-gate * Returns -1 if the date is out of range.
11587c478bd9Sstevel@tonic-gate */
11597c478bd9Sstevel@tonic-gate static time_t
hs_date_to_gmtime(int year,int mon,int day,int gmtoff)11607c478bd9Sstevel@tonic-gate hs_date_to_gmtime(int year, int mon, int day, int gmtoff)
11617c478bd9Sstevel@tonic-gate {
11627c478bd9Sstevel@tonic-gate time_t sum;
11637c478bd9Sstevel@tonic-gate time_t *cp;
11647c478bd9Sstevel@tonic-gate int y;
11657c478bd9Sstevel@tonic-gate
11667c478bd9Sstevel@tonic-gate if ((year < THE_EPOCH) || (year > END_OF_TIME) ||
11677c478bd9Sstevel@tonic-gate (mon < 1) || (mon > 12) ||
11687c478bd9Sstevel@tonic-gate (day < 1) || (day > 31))
11697c478bd9Sstevel@tonic-gate return (-1);
11707c478bd9Sstevel@tonic-gate
11717c478bd9Sstevel@tonic-gate /*
11727c478bd9Sstevel@tonic-gate * Figure seconds until this year and correct for leap years.
11737c478bd9Sstevel@tonic-gate * Note: 2000 is a leap year but not 2100.
11747c478bd9Sstevel@tonic-gate */
11757c478bd9Sstevel@tonic-gate y = year - THE_EPOCH;
11767c478bd9Sstevel@tonic-gate sum = y * SEC_PER_YEAR;
11777c478bd9Sstevel@tonic-gate sum += ((y + 1) / 4) * SEC_PER_DAY;
11787c478bd9Sstevel@tonic-gate /*
11797c478bd9Sstevel@tonic-gate * Point to the correct table for this year and
11807c478bd9Sstevel@tonic-gate * add in seconds until this month.
11817c478bd9Sstevel@tonic-gate */
11827c478bd9Sstevel@tonic-gate cp = ((y + 2) % 4) ? cum_sec : cum_sec_leap;
11837c478bd9Sstevel@tonic-gate sum += cp[mon - 1];
11847c478bd9Sstevel@tonic-gate /*
11857c478bd9Sstevel@tonic-gate * Add in seconds until 0:00 of this day.
11867c478bd9Sstevel@tonic-gate * (days-per-month validation is not done here)
11877c478bd9Sstevel@tonic-gate */
11887c478bd9Sstevel@tonic-gate sum += (day - 1) * SEC_PER_DAY;
11897c478bd9Sstevel@tonic-gate sum -= (gmtoff * 15 * 60);
11907c478bd9Sstevel@tonic-gate return (sum);
11917c478bd9Sstevel@tonic-gate }
1192