ufs_module.c revision 294060
1294060Ssmh/*-
2294060Ssmh * Copyright (c) 1998 Robert Nordier
3294060Ssmh * All rights reserved.
4294060Ssmh * Copyright (c) 2001 Robert Drehmel
5294060Ssmh * All rights reserved.
6294060Ssmh * Copyright (c) 2014 Nathan Whitehorn
7294060Ssmh * All rights reserved.
8294060Ssmh * Copyright (c) 2015 Eric McCorkle
9294060Ssmh * All rights reverved.
10294060Ssmh *
11294060Ssmh * Redistribution and use in source and binary forms, with or without
12294060Ssmh * modification, are permitted provided that the following conditions
13294060Ssmh * are met:
14294060Ssmh * 1. Redistributions of source code must retain the above copyright
15294060Ssmh *    notice, this list of conditions and the following disclaimer.
16294060Ssmh * 2. Redistributions in binary form must reproduce the above copyright
17294060Ssmh *    notice, this list of conditions and the following disclaimer in the
18294060Ssmh *    documentation and/or other materials provided with the distribution.
19294060Ssmh *
20294060Ssmh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21294060Ssmh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22294060Ssmh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23294060Ssmh * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24294060Ssmh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25294060Ssmh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26294060Ssmh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27294060Ssmh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28294060Ssmh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29294060Ssmh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30294060Ssmh * SUCH DAMAGE.
31294060Ssmh *
32294060Ssmh * $FreeBSD: head/sys/boot/efi/boot1/ufs_module.c 294060 2016-01-15 01:22:36Z smh $
33294060Ssmh */
34294060Ssmh
35294060Ssmh#include <stdarg.h>
36294060Ssmh#include <stdbool.h>
37294060Ssmh#include <sys/cdefs.h>
38294060Ssmh#include <sys/param.h>
39294060Ssmh#include <efi.h>
40294060Ssmh
41294060Ssmh#include "boot_module.h"
42294060Ssmh
43294060Ssmhstatic dev_info_t *devinfo;
44294060Ssmhstatic dev_info_t *devices;
45294060Ssmh
46294060Ssmhstatic int
47294060Ssmhdskread(void *buf, u_int64_t lba, int nblk)
48294060Ssmh{
49294060Ssmh	int size;
50294060Ssmh	EFI_STATUS status;
51294060Ssmh
52294060Ssmh	lba = lba / (devinfo->dev->Media->BlockSize / DEV_BSIZE);
53294060Ssmh	size = nblk * DEV_BSIZE;
54294060Ssmh
55294060Ssmh	status = devinfo->dev->ReadBlocks(devinfo->dev,
56294060Ssmh	    devinfo->dev->Media->MediaId, lba, size, buf);
57294060Ssmh
58294060Ssmh	if (status != EFI_SUCCESS) {
59294060Ssmh		DPRINTF("dskread: failed dev: %p, id: %u, lba: %lu, size: %d, "
60294060Ssmh		    "status: %lu\n", devinfo->dev,
61294060Ssmh		    devinfo->dev->Media->MediaId, lba, size,
62294060Ssmh		    EFI_ERROR_CODE(status));
63294060Ssmh		return (-1);
64294060Ssmh	}
65294060Ssmh
66294060Ssmh	return (0);
67294060Ssmh}
68294060Ssmh
69294060Ssmh#include "ufsread.c"
70294060Ssmh
71294060Ssmhstatic ssize_t
72294060Ssmhfsstat(ufs_ino_t inode)
73294060Ssmh{
74294060Ssmh#ifndef UFS2_ONLY
75294060Ssmh	static struct ufs1_dinode dp1;
76294060Ssmh#endif
77294060Ssmh#ifndef UFS1_ONLY
78294060Ssmh	static struct ufs2_dinode dp2;
79294060Ssmh#endif
80294060Ssmh	static struct fs fs;
81294060Ssmh	static ufs_ino_t inomap;
82294060Ssmh	char *blkbuf;
83294060Ssmh	void *indbuf;
84294060Ssmh	size_t n, size;
85294060Ssmh	static ufs2_daddr_t blkmap, indmap;
86294060Ssmh
87294060Ssmh	blkbuf = dmadat->blkbuf;
88294060Ssmh	indbuf = dmadat->indbuf;
89294060Ssmh	if (!dsk_meta) {
90294060Ssmh		inomap = 0;
91294060Ssmh		for (n = 0; sblock_try[n] != -1; n++) {
92294060Ssmh			if (dskread(dmadat->sbbuf, sblock_try[n] / DEV_BSIZE,
93294060Ssmh			    SBLOCKSIZE / DEV_BSIZE))
94294060Ssmh				return (-1);
95294060Ssmh			memcpy(&fs, dmadat->sbbuf, sizeof(struct fs));
96294060Ssmh			if ((
97294060Ssmh#if defined(UFS1_ONLY)
98294060Ssmh			    fs.fs_magic == FS_UFS1_MAGIC
99294060Ssmh#elif defined(UFS2_ONLY)
100294060Ssmh			    (fs.fs_magic == FS_UFS2_MAGIC &&
101294060Ssmh			    fs.fs_sblockloc == sblock_try[n])
102294060Ssmh#else
103294060Ssmh			    fs.fs_magic == FS_UFS1_MAGIC ||
104294060Ssmh			    (fs.fs_magic == FS_UFS2_MAGIC &&
105294060Ssmh			    fs.fs_sblockloc == sblock_try[n])
106294060Ssmh#endif
107294060Ssmh			    ) &&
108294060Ssmh			    fs.fs_bsize <= MAXBSIZE &&
109294060Ssmh			    fs.fs_bsize >= (int32_t)sizeof(struct fs))
110294060Ssmh				break;
111294060Ssmh		}
112294060Ssmh		if (sblock_try[n] == -1) {
113294060Ssmh			return (-1);
114294060Ssmh		}
115294060Ssmh		dsk_meta++;
116294060Ssmh	} else
117294060Ssmh		memcpy(&fs, dmadat->sbbuf, sizeof(struct fs));
118294060Ssmh	if (!inode)
119294060Ssmh		return (0);
120294060Ssmh	if (inomap != inode) {
121294060Ssmh		n = IPERVBLK(&fs);
122294060Ssmh		if (dskread(blkbuf, INO_TO_VBA(&fs, n, inode), DBPERVBLK))
123294060Ssmh			return (-1);
124294060Ssmh		n = INO_TO_VBO(n, inode);
125294060Ssmh#if defined(UFS1_ONLY)
126294060Ssmh		memcpy(&dp1, (struct ufs1_dinode *)blkbuf + n,
127294060Ssmh		    sizeof(struct ufs1_dinode));
128294060Ssmh#elif defined(UFS2_ONLY)
129294060Ssmh		memcpy(&dp2, (struct ufs2_dinode *)blkbuf + n,
130294060Ssmh		    sizeof(struct ufs2_dinode));
131294060Ssmh#else
132294060Ssmh		if (fs.fs_magic == FS_UFS1_MAGIC)
133294060Ssmh			memcpy(&dp1, (struct ufs1_dinode *)(void *)blkbuf + n,
134294060Ssmh			    sizeof(struct ufs1_dinode));
135294060Ssmh		else
136294060Ssmh			memcpy(&dp2, (struct ufs2_dinode *)(void *)blkbuf + n,
137294060Ssmh			    sizeof(struct ufs2_dinode));
138294060Ssmh#endif
139294060Ssmh		inomap = inode;
140294060Ssmh		fs_off = 0;
141294060Ssmh		blkmap = indmap = 0;
142294060Ssmh	}
143294060Ssmh	size = DIP(di_size);
144294060Ssmh	n = size - fs_off;
145294060Ssmh
146294060Ssmh	return (n);
147294060Ssmh}
148294060Ssmh
149294060Ssmhstatic struct dmadat __dmadat;
150294060Ssmh
151294060Ssmhstatic EFI_STATUS
152294060Ssmhprobe(dev_info_t* dev)
153294060Ssmh{
154294060Ssmh
155294060Ssmh	devinfo = dev;
156294060Ssmh	dmadat = &__dmadat;
157294060Ssmh	if (fsread(0, NULL, 0) < 0)
158294060Ssmh		return (EFI_UNSUPPORTED);
159294060Ssmh
160294060Ssmh	add_device(&devices, dev);
161294060Ssmh
162294060Ssmh	return (EFI_SUCCESS);
163294060Ssmh}
164294060Ssmh
165294060Ssmhstatic EFI_STATUS
166294060Ssmhtry_load(dev_info_t *dev, const char *loader_path, void **bufp, size_t *bufsize)
167294060Ssmh{
168294060Ssmh	ufs_ino_t ino;
169294060Ssmh	EFI_STATUS status;
170294060Ssmh	size_t size;
171294060Ssmh	ssize_t read;
172294060Ssmh	void *buf;
173294060Ssmh
174294060Ssmh	devinfo = dev;
175294060Ssmh	if ((ino = lookup(loader_path)) == 0)
176294060Ssmh		return (EFI_NOT_FOUND);
177294060Ssmh
178294060Ssmh	size = fsstat(ino);
179294060Ssmh	if (size <= 0) {
180294060Ssmh		printf("Failed to fsstat %s ino: %d\n", loader_path, ino);
181294060Ssmh		return (EFI_INVALID_PARAMETER);
182294060Ssmh	}
183294060Ssmh
184294060Ssmh	if ((status = bs->AllocatePool(EfiLoaderData, size, &buf)) !=
185294060Ssmh	    EFI_SUCCESS) {
186294060Ssmh		printf("Failed to allocate read buffer (%lu)\n",
187294060Ssmh		    EFI_ERROR_CODE(status));
188294060Ssmh		return (status);
189294060Ssmh	}
190294060Ssmh
191294060Ssmh	read = fsread(ino, buf, size);
192294060Ssmh	if ((size_t)read != size) {
193294060Ssmh		printf("Failed to read %s (%zd != %zu)\n", loader_path, read,
194294060Ssmh		    size);
195294060Ssmh		(void)bs->FreePool(buf);
196294060Ssmh		return (EFI_INVALID_PARAMETER);
197294060Ssmh	}
198294060Ssmh
199294060Ssmh	*bufp = buf;
200294060Ssmh	*bufsize = size;
201294060Ssmh
202294060Ssmh	return (EFI_SUCCESS);
203294060Ssmh}
204294060Ssmh
205294060Ssmhstatic EFI_STATUS
206294060Ssmhload(const char *loader_path, dev_info_t **devinfop, void **buf,
207294060Ssmh    size_t *bufsize)
208294060Ssmh{
209294060Ssmh	dev_info_t *dev;
210294060Ssmh	EFI_STATUS status;
211294060Ssmh
212294060Ssmh	for (dev = devices; dev != NULL; dev = dev->next) {
213294060Ssmh		status = try_load(dev, loader_path, buf, bufsize);
214294060Ssmh		if (status == EFI_SUCCESS) {
215294060Ssmh			*devinfop = dev;
216294060Ssmh			return (EFI_SUCCESS);
217294060Ssmh		} else if (status != EFI_NOT_FOUND) {
218294060Ssmh			return (status);
219294060Ssmh		}
220294060Ssmh	}
221294060Ssmh
222294060Ssmh	return (EFI_NOT_FOUND);
223294060Ssmh}
224294060Ssmh
225294060Ssmhstatic void
226294060Ssmhstatus()
227294060Ssmh{
228294060Ssmh	int i;
229294060Ssmh	dev_info_t *dev;
230294060Ssmh
231294060Ssmh	for (dev = devices, i = 0; dev != NULL; dev = dev->next, i++)
232294060Ssmh		;
233294060Ssmh
234294060Ssmh	printf("%s found ", ufs_module.name);
235294060Ssmh	switch (i) {
236294060Ssmh	case 0:
237294060Ssmh		printf("no partitions\n");
238294060Ssmh		break;
239294060Ssmh	case 1:
240294060Ssmh		printf("%d partition\n", i);
241294060Ssmh		break;
242294060Ssmh	default:
243294060Ssmh		printf("%d partitions\n", i);
244294060Ssmh	}
245294060Ssmh}
246294060Ssmh
247294060Ssmhconst boot_module_t ufs_module =
248294060Ssmh{
249294060Ssmh	.name = "UFS",
250294060Ssmh	.probe = probe,
251294060Ssmh	.load = load,
252294060Ssmh	.status = status
253294060Ssmh};
254