boot: Adjust some printfs which take unsigned but had %d.
[dragonfly.git] / sys / boot / common / ufsread.c
1 /*-
2  * Copyright (c) 2002 Networks Associates Technology, Inc.
3  * All rights reserved.
4  *
5  * This software was developed for the FreeBSD Project by Marshall
6  * Kirk McKusick and Network Associates Laboratories, the Security
7  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
9  * research program
10  *
11  * Copyright (c) 1998 Robert Nordier
12  * All rights reserved.
13  *
14  * Redistribution and use in source and binary forms are freely
15  * permitted provided that the above copyright notice and this
16  * paragraph and the following disclaimer are duplicated in all
17  * such forms.
18  *
19  * This software is provided "AS IS" and without any express or
20  * implied warranties, including, without limitation, the implied
21  * warranties of merchantability and fitness for a particular
22  * purpose.
23  *
24  * $FreeBSD: src/sys/boot/common/ufsread.c,v 1.12 2003/08/25 23:30:41 obrien Exp $
25  * $DragonFly: src/sys/boot/common/ufsread.c,v 1.5 2008/09/13 11:46:28 corecode Exp $
26  */
27
28 #ifdef BOOT2
29 #include "boot2.h"
30 #else
31 #include <sys/param.h>
32 #endif
33 #include <sys/dtype.h>
34 #include <sys/dirent.h>
35 #include <machine/bootinfo.h>
36 #include <machine/elf.h>
37 #include <vfs/ufs/dir.h>
38 #include "dinode.h"
39 #include "fs.h"
40
41 #ifdef __i386__
42 /* XXX: Revert to old (broken for over 1.5Tb filesystems) version of cgbase
43    (see sys/ufs/ffs/fs.h rev 1.39) so that i386 boot loader (boot2) can
44    support both UFS1 and UFS2 again. */
45 #undef cgbase
46 #define cgbase(fs, c)   ((ufs2_daddr_t)((fs)->fs_fpg * (c)))
47 #endif
48
49 /*
50  * We use 4k `virtual' blocks for filesystem data, whatever the actual
51  * filesystem block size. FFS blocks are always a multiple of 4k.
52  */
53 #define VBLKSHIFT       12
54 #define VBLKSIZE        (1 << VBLKSHIFT)
55 #define VBLKMASK        (VBLKSIZE - 1)
56 #define DBPERVBLK       (VBLKSIZE / DEV_BSIZE)
57 #define INDIRPERVBLK(fs) (NINDIR(fs) / ((fs)->fs_bsize >> VBLKSHIFT))
58 #define IPERVBLK(fs)    (INOPB(fs) / ((fs)->fs_bsize >> VBLKSHIFT))
59 #define INO_TO_VBA(fs, ipervblk, x) \
60     (fsbtodb(fs, cgimin(fs, ino_to_cg(fs, x))) + \
61     (((x) % (fs)->fs_ipg) / (ipervblk) * DBPERVBLK))
62 #define INO_TO_VBO(ipervblk, x) ((x) % ipervblk)
63 #define FS_TO_VBA(fs, fsb, off) (fsbtodb(fs, fsb) + \
64     ((off) / VBLKSIZE) * DBPERVBLK)
65 #define FS_TO_VBO(fs, fsb, off) ((off) & VBLKMASK)
66
67 /* Buffers that must not span a 64k boundary. */
68 struct ufs_dmadat {
69         struct boot2_dmadat boot2;
70         char blkbuf[VBLKSIZE];  /* filesystem blocks */
71         char indbuf[VBLKSIZE];  /* indir blocks */
72         char sbbuf[SBLOCKSIZE]; /* superblock */
73 };
74
75 #define fsdmadat        ((struct ufs_dmadat *)boot2_dmadat)
76
77 static boot2_ino_t boot2_ufs_lookup(const char *);
78 static ssize_t boot2_ufs_read(boot2_ino_t, void *, size_t);
79 static int boot2_ufs_init(void);
80
81 const struct boot2_fsapi boot2_ufs_api = {
82         .fsinit = boot2_ufs_init,
83         .fslookup = boot2_ufs_lookup,
84         .fsread = boot2_ufs_read
85 };
86
87 static __inline__ int
88 fsfind(const char *name, ufs_ino_t *ino)
89 {
90         char buf[DEV_BSIZE];
91         struct direct *d;
92         char *s;
93         ssize_t n;
94
95         fs_off = 0;
96         while ((n = boot2_ufs_read(*ino, buf, DEV_BSIZE)) > 0)
97                 for (s = buf; s < buf + DEV_BSIZE;) {
98                         d = (void *)s;
99                         if (ls)
100                                 printf("%s ", d->d_name);
101                         else if (!strcmp(name, d->d_name)) {
102                                 *ino = d->d_ino;
103                                 return d->d_type;
104                         }
105                         s += d->d_reclen;
106                 }
107         if (n != -1 && ls)
108                 printf("\n");
109         return 0;
110 }
111
112 static boot2_ino_t
113 boot2_ufs_lookup(const char *path)
114 {
115         char name[MAXNAMLEN + 1];
116         const char *s;
117         ufs_ino_t ino;
118         ssize_t n;
119         int dt;
120
121         ino = ROOTINO;
122         dt = DT_DIR;
123         name[0] = '/';
124         name[1] = '\0';
125         for (;;) {
126                 if (*path == '/')
127                         path++;
128                 if (!*path)
129                         break;
130                 for (s = path; *s && *s != '/'; s++);
131                 if ((n = s - path) > MAXNAMLEN)
132                         return 0;
133                 ls = *path == '?' && n == 1 && !*s;
134                 memcpy(name, path, n);
135                 name[n] = 0;
136                 if (dt != DT_DIR) {
137                         printf("%s: not a directory.\n", name);
138                         return (0);
139                 }
140                 if ((dt = fsfind(name, &ino)) <= 0)
141                         break;
142                 path = s;
143         }
144         return dt == DT_REG ? ino : 0;
145 }
146
147 /*
148  * Possible superblock locations ordered from most to least likely.
149  */
150 static int sblock_try[] = SBLOCKSEARCH;
151
152 #if defined(UFS2_ONLY)
153 #define DIP(field) dp2.field
154 #elif defined(UFS1_ONLY)
155 #define DIP(field) dp1.field
156 #else
157 #define DIP(field) fs->fs_magic == FS_UFS1_MAGIC ? dp1.field : dp2.field
158 #endif
159
160 static ufs_ino_t inomap;
161 static ufs2_daddr_t blkmap, indmap;
162
163 static int
164 boot2_ufs_init(void)
165 {
166         struct fs *fs;
167         size_t n;
168
169         inomap = 0;
170         fs = (struct fs *)fsdmadat->sbbuf;
171
172         for (n = 0; sblock_try[n] != -1; n++) {
173                 if (dskread(fs, sblock_try[n] / DEV_BSIZE,
174                             SBLOCKSIZE / DEV_BSIZE)) {
175                         return -1;
176                 }
177                 if ((
178 #if defined(UFS1_ONLY)
179                      fs->fs_magic == FS_UFS1_MAGIC
180 #elif defined(UFS2_ONLY)
181                     (fs->fs_magic == FS_UFS2_MAGIC &&
182                     fs->fs_sblockloc == sblock_try[n])
183 #else
184                      fs->fs_magic == FS_UFS1_MAGIC ||
185                     (fs->fs_magic == FS_UFS2_MAGIC &&
186                     fs->fs_sblockloc == sblock_try[n])
187 #endif
188                     ) &&
189                     fs->fs_bsize <= MAXBSIZE &&
190                     fs->fs_bsize >= sizeof(struct fs))
191                         break;
192         }
193         if (sblock_try[n] == -1)
194                 return -1;
195         return 0;
196 }
197
198 static ssize_t
199 boot2_ufs_read(boot2_ino_t boot2_inode, void *buf, size_t nbyte)
200 {
201 #ifndef UFS2_ONLY
202         static struct ufs1_dinode dp1;
203 #endif
204 #ifndef UFS1_ONLY
205         static struct ufs2_dinode dp2;
206 #endif
207         ufs_ino_t ufs_inode = (ufs_ino_t)boot2_inode;
208         char *blkbuf;
209         void *indbuf;
210         struct fs *fs;
211         char *s;
212         size_t n, nb, size, off, vboff;
213         ufs_lbn_t lbn;
214         ufs2_daddr_t addr, vbaddr;
215         u_int u;
216
217         blkbuf = fsdmadat->blkbuf;
218         indbuf = fsdmadat->indbuf;
219         fs = (struct fs *)fsdmadat->sbbuf;
220
221         if (!ufs_inode)
222                 return 0;
223         if (inomap != ufs_inode) {
224                 n = IPERVBLK(fs);
225                 if (dskread(blkbuf, INO_TO_VBA(fs, n, ufs_inode), DBPERVBLK))
226                         return -1;
227                 n = INO_TO_VBO(n, ufs_inode);
228 #if defined(UFS1_ONLY)
229                 dp1 = ((struct ufs1_dinode *)blkbuf)[n];
230 #elif defined(UFS2_ONLY)
231                 dp2 = ((struct ufs2_dinode *)blkbuf)[n];
232 #else
233                 if (fs->fs_magic == FS_UFS1_MAGIC)
234                         dp1 = ((struct ufs1_dinode *)blkbuf)[n];
235                 else
236                         dp2 = ((struct ufs2_dinode *)blkbuf)[n];
237 #endif
238                 inomap = ufs_inode;
239                 fs_off = 0;
240                 blkmap = indmap = 0;
241         }
242         s = buf;
243         size = DIP(di_size);
244         n = size - fs_off;
245         if (nbyte > n)
246                 nbyte = n;
247         nb = nbyte;
248         while (nb) {
249                 lbn = lblkno(fs, fs_off);
250                 off = blkoff(fs, fs_off);
251                 if (lbn < NDADDR) {
252                         addr = DIP(di_db[lbn]);
253                 } else if (lbn < NDADDR + NINDIR(fs)) {
254                         n = INDIRPERVBLK(fs);
255                         addr = DIP(di_ib[0]);
256                         u = (u_int)(lbn - NDADDR) / n * DBPERVBLK;
257                         vbaddr = fsbtodb(fs, addr) + u;
258                         if (indmap != vbaddr) {
259                                 if (dskread(indbuf, vbaddr, DBPERVBLK))
260                                         return -1;
261                                 indmap = vbaddr;
262                         }
263                         n = (lbn - NDADDR) & (n - 1);
264 #if defined(UFS1_ONLY)
265                         addr = ((ufs1_daddr_t *)indbuf)[n];
266 #elif defined(UFS2_ONLY)
267                         addr = ((ufs2_daddr_t *)indbuf)[n];
268 #else
269                         if (fs->fs_magic == FS_UFS1_MAGIC)
270                                 addr = ((ufs1_daddr_t *)indbuf)[n];
271                         else
272                                 addr = ((ufs2_daddr_t *)indbuf)[n];
273 #endif
274                 } else {
275                         return -1;
276                 }
277                 vbaddr = fsbtodb(fs, addr) + (off >> VBLKSHIFT) * DBPERVBLK;
278                 vboff = off & VBLKMASK;
279                 n = sblksize(fs, size, lbn) - (off & ~VBLKMASK);
280                 if (n > VBLKSIZE)
281                         n = VBLKSIZE;
282                 if (blkmap != vbaddr) {
283                         if (dskread(blkbuf, vbaddr, n >> DEV_BSHIFT))
284                                 return -1;
285                         blkmap = vbaddr;
286                 }
287                 n -= vboff;
288                 if (n > nb)
289                         n = nb;
290                 memcpy(s, blkbuf + vboff, n);
291                 s += n;
292                 fs_off += n;
293                 nb -= n;
294         }
295         return nbyte;
296 }