aboutsummaryrefslogtreecommitdiffstats
path: root/tools/libfsimage/ufs/fsys_ufs.c
blob: be5141125742da54ed4e96fa04c13eca535a9b2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*  
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2006 Free Software Foundation, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/* From Solaris usr/src/stand/lib/fs/ufs/ufsops.c */

#include <fsimage_grub.h>

#include "ufs.h"

/* These are the pools of buffers, etc. */

#define SUPERBLOCK ((struct fs *)(FSYS_BUF + 0x2000))
#define	INODE ((struct icommon *)(FSYS_BUF + 0x1000))
#define DIRENT (FSYS_BUF + 0x4000)
#define MAXBSIZE ((FSYS_BUFLEN - 0x4000) / 2)
#define INDIRBLK1 ((grub_daddr32_t *)(FSYS_BUF + 0x4000)) /* 2+ indir blk */
#define	INDIRBLK0 ((grub_daddr32_t *)(FSYS_BUF+ 0x4000 + MAXBSIZE))  /* 1st indirect blk */

#define	indirblk0 (*fsig_int1(ffi))
#define	indirblk1 (*fsig_int2(ffi))

static int openi(fsi_file_t *, grub_ino_t);
static grub_ino_t dlook(fsi_file_t *, grub_ino_t, char *);
static grub_daddr32_t sbmap(fsi_file_t *, grub_daddr32_t);

/* read superblock and check fs magic */
static int
ufs_mount(fsi_file_t *ffi, const char *options)
{
	if (/*! IS_PC_SLICE_TYPE_SOLARIS(current_slice) || */
	    !devread(ffi, UFS_SBLOCK, 0, UFS_SBSIZE, (char *)SUPERBLOCK) ||
	    SUPERBLOCK->fs_magic != UFS_MAGIC ||
	    MAXBSIZE < SUPERBLOCK->fs_bsize)
		return 0;

	return 1;
}


/*
 * searching for a file, if successful, inode will be loaded in INODE
 * The entry point should really be named ufs_open(char *pathname).
 * For now, keep it consistent with the rest of fsys modules.
 */
static int
ufs_dir(fsi_file_t *ffi, char *dirname)
{
	grub_ino_t inode = ROOTINO;	/* start from root */
	char *fname, ch;

	indirblk0 = indirblk1 = 0;

	/* skip leading slashes */
	while (*dirname == '/')
		dirname++;

	while (inode && *dirname && !isspace((uint8_t)*dirname)) {
		if (!openi(ffi, inode))
			return 0;

		/* parse for next path component */
		fname = dirname;
		while (*dirname && !isspace((uint8_t)*dirname) && *dirname != '/')
			dirname++;
		ch = *dirname;
		*dirname = 0;	/* ensure null termination */

		inode = dlook(ffi, inode, fname);
		*dirname = ch;
		while (*dirname == '/')
			dirname++;
	}

	/* return 1 only if inode exists and is a regular file */
	if  (! openi(ffi, inode))
		return (0);
	filepos = 0;
	filemax = INODE->ic_sizelo;
	return (inode && ((INODE->ic_smode & IFMT) == IFREG));
}

/*
 * This is the high-level read function.
 */
static int
ufs_read(fsi_file_t *ffi, char *buf, int len)
{
  	int off, size, ret = 0, ok;
	grub_daddr32_t lblk, dblk;

  	while (len) {
	  	off = blkoff(SUPERBLOCK, filepos);
		lblk = lblkno(SUPERBLOCK, filepos);
		size = SUPERBLOCK->fs_bsize;
		size -= off;
		if (size > len)
		  	size = len;

		if ((dblk = sbmap(ffi, lblk)) <= 0) {
			/* we are in a file hole, just zero the buf */
			grub_memset(buf, 0, size);
		} else {
			disk_read_func = disk_read_hook;
			ok = devread(ffi, fsbtodb(SUPERBLOCK, dblk),
				off, size, buf);
			disk_read_func = 0;
			if (!ok)
		  		return 0;
		}
		buf += size;
		len -= size;
		filepos += size;
		ret += size;
	}

	return (ret);
}

int
ufs_embed (int *start_sector, int needed_sectors)
{
	if (needed_sectors > 14)
        	return 0;

	*start_sector = 2;
	return 1;
}

/* read inode and place content in INODE */
static int
openi(fsi_file_t *ffi, grub_ino_t inode)
{
	grub_daddr32_t dblk;
	int off;

	/* get block and byte offset into the block */
	dblk = fsbtodb(SUPERBLOCK, itod(SUPERBLOCK, inode));
	off = itoo(SUPERBLOCK, inode) * sizeof (struct icommon);

	return (devread(ffi, dblk, off, sizeof (struct icommon), (char *)INODE));
}

/*
 * Performs fileblock mapping. Convert file block no. to disk block no.
 * Returns 0 when block doesn't exist and <0 when block isn't initialized
 * (i.e belongs to a hole in the file).
 */
grub_daddr32_t
sbmap(fsi_file_t *ffi, grub_daddr32_t bn)
{
  	int level, bound, i, index;
	grub_daddr32_t nb, blkno;
	grub_daddr32_t *db = INODE->ic_db;

	/* blocks 0..UFS_NDADDR are direct blocks */
	if (bn < UFS_NDADDR) {
		return db[bn];
	}

	/* determine how many levels of indirection. */
	level = 0;
	bn -= UFS_NDADDR;
	bound = UFS_NINDIR(SUPERBLOCK);
	while (bn >= bound) {
		level++;
		bn -= bound;
		bound *= UFS_NINDIR(SUPERBLOCK);
	}
	if (level >= UFS_NIADDR)	/* bn too big */
		return ((grub_daddr32_t)0);

	/* fetch the first indirect block */
	nb = INODE->ic_ib[level];
	if (nb == 0) {
		return ((grub_daddr32_t)0);
	}
	if (indirblk0 != nb) {
		indirblk0 = 0;
		blkno = fsbtodb(SUPERBLOCK, nb);
		if (!devread(ffi, blkno, 0, SUPERBLOCK->fs_bsize,
		    (char *)INDIRBLK0))
			return (0);
		indirblk0 = nb;
	}
	bound /= UFS_NINDIR(SUPERBLOCK);
	index = (bn / bound) % UFS_NINDIR(SUPERBLOCK);
	nb = INDIRBLK0[index];

	/* fetch through the indirect blocks */
	for (i = 1; i <= level; i++) {
		if (indirblk1 != nb) {
			blkno = fsbtodb(SUPERBLOCK, nb);
			if (!devread(ffi, blkno, 0, SUPERBLOCK->fs_bsize,
			    (char *)INDIRBLK1))
				return (0);
			indirblk1 = nb;
		}
		bound /= UFS_NINDIR(SUPERBLOCK);
		index = (bn / bound) % UFS_NINDIR(SUPERBLOCK);
		nb = INDIRBLK1[index];
		if (nb == 0)
			return ((grub_daddr32_t)0);
	}

	return (nb);
}

/* search directory content for name, return inode number */
static grub_ino_t
dlook(fsi_file_t *ffi, grub_ino_t dir_ino, char *name)
{
	int loc, off;
	grub_daddr32_t lbn, dbn, dblk;
	struct direct *dp;

	if ((INODE->ic_smode & IFMT) != IFDIR)
		return 0;

	loc = 0;
	while (loc < INODE->ic_sizelo) {
	  	/* offset into block */
		off = blkoff(SUPERBLOCK, loc);
		if (off == 0) {		/* need to read in a new block */
		  	/* get logical block number */
			lbn = lblkno(SUPERBLOCK, loc);
			/* resolve indrect blocks */
			dbn = sbmap(ffi, lbn);
			if (dbn == 0)
				return (0);

			dblk = fsbtodb(SUPERBLOCK, dbn);
			if (!devread(ffi, dblk, 0, SUPERBLOCK->fs_bsize,
			    (char *)DIRENT)) {
				return 0;
			}
		}

		dp = (struct direct *)(DIRENT + off);
		if (dp->d_ino && substring(name, dp->d_name) == 0)
			return (dp->d_ino);
		loc += dp->d_reclen;
	}
	return (0);
}

fsi_plugin_ops_t *
fsi_init_plugin(int version, fsi_plugin_t *fp, const char **name)
{
	static fsig_plugin_ops_t ops = {
		FSIMAGE_PLUGIN_VERSION,
		.fpo_mount = ufs_mount,
		.fpo_dir = ufs_dir,
		.fpo_read = ufs_read
	};

	*name = "ufs";
	return (fsig_init(fp, &ops));
}