ufs_module.c revision 294493
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 294493 2016-01-21 08:58:39Z 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
71294493Ssmhstatic struct dmadat __dmadat;
72294493Ssmh
73294493Ssmhstatic int
74294493Ssmhinit_dev(dev_info_t* dev)
75294060Ssmh{
76294060Ssmh
77294493Ssmh	devinfo = dev;
78294493Ssmh	dmadat = &__dmadat;
79294060Ssmh
80294493Ssmh	return fsread(0, NULL, 0);
81294060Ssmh}
82294060Ssmh
83294060Ssmhstatic EFI_STATUS
84294060Ssmhprobe(dev_info_t* dev)
85294060Ssmh{
86294060Ssmh
87294493Ssmh	if (init_dev(dev) < 0)
88294060Ssmh		return (EFI_UNSUPPORTED);
89294060Ssmh
90294060Ssmh	add_device(&devices, dev);
91294060Ssmh
92294060Ssmh	return (EFI_SUCCESS);
93294060Ssmh}
94294060Ssmh
95294060Ssmhstatic EFI_STATUS
96294060Ssmhtry_load(dev_info_t *dev, const char *loader_path, void **bufp, size_t *bufsize)
97294060Ssmh{
98294060Ssmh	ufs_ino_t ino;
99294060Ssmh	EFI_STATUS status;
100294060Ssmh	size_t size;
101294060Ssmh	ssize_t read;
102294060Ssmh	void *buf;
103294060Ssmh
104294493Ssmh	if (init_dev(dev) < 0)
105294493Ssmh		return (EFI_UNSUPPORTED);
106294493Ssmh
107294060Ssmh	if ((ino = lookup(loader_path)) == 0)
108294060Ssmh		return (EFI_NOT_FOUND);
109294060Ssmh
110294493Ssmh	if (fsread_size(ino, NULL, 0, &size) < 0 || size <= 0) {
111294493Ssmh		printf("Failed to read size of '%s' ino: %d\n", loader_path,
112294493Ssmh		    ino);
113294060Ssmh		return (EFI_INVALID_PARAMETER);
114294060Ssmh	}
115294060Ssmh
116294060Ssmh	if ((status = bs->AllocatePool(EfiLoaderData, size, &buf)) !=
117294060Ssmh	    EFI_SUCCESS) {
118294060Ssmh		printf("Failed to allocate read buffer (%lu)\n",
119294060Ssmh		    EFI_ERROR_CODE(status));
120294060Ssmh		return (status);
121294060Ssmh	}
122294060Ssmh
123294060Ssmh	read = fsread(ino, buf, size);
124294060Ssmh	if ((size_t)read != size) {
125294493Ssmh		printf("Failed to read '%s' (%zd != %zu)\n", loader_path, read,
126294060Ssmh		    size);
127294060Ssmh		(void)bs->FreePool(buf);
128294060Ssmh		return (EFI_INVALID_PARAMETER);
129294060Ssmh	}
130294060Ssmh
131294060Ssmh	*bufp = buf;
132294060Ssmh	*bufsize = size;
133294060Ssmh
134294060Ssmh	return (EFI_SUCCESS);
135294060Ssmh}
136294060Ssmh
137294060Ssmhstatic EFI_STATUS
138294060Ssmhload(const char *loader_path, dev_info_t **devinfop, void **buf,
139294060Ssmh    size_t *bufsize)
140294060Ssmh{
141294060Ssmh	dev_info_t *dev;
142294060Ssmh	EFI_STATUS status;
143294060Ssmh
144294060Ssmh	for (dev = devices; dev != NULL; dev = dev->next) {
145294060Ssmh		status = try_load(dev, loader_path, buf, bufsize);
146294060Ssmh		if (status == EFI_SUCCESS) {
147294060Ssmh			*devinfop = dev;
148294060Ssmh			return (EFI_SUCCESS);
149294060Ssmh		} else if (status != EFI_NOT_FOUND) {
150294060Ssmh			return (status);
151294060Ssmh		}
152294060Ssmh	}
153294060Ssmh
154294060Ssmh	return (EFI_NOT_FOUND);
155294060Ssmh}
156294060Ssmh
157294060Ssmhstatic void
158294060Ssmhstatus()
159294060Ssmh{
160294060Ssmh	int i;
161294060Ssmh	dev_info_t *dev;
162294060Ssmh
163294060Ssmh	for (dev = devices, i = 0; dev != NULL; dev = dev->next, i++)
164294060Ssmh		;
165294060Ssmh
166294060Ssmh	printf("%s found ", ufs_module.name);
167294060Ssmh	switch (i) {
168294060Ssmh	case 0:
169294060Ssmh		printf("no partitions\n");
170294060Ssmh		break;
171294060Ssmh	case 1:
172294060Ssmh		printf("%d partition\n", i);
173294060Ssmh		break;
174294060Ssmh	default:
175294060Ssmh		printf("%d partitions\n", i);
176294060Ssmh	}
177294060Ssmh}
178294060Ssmh
179294060Ssmhconst boot_module_t ufs_module =
180294060Ssmh{
181294060Ssmh	.name = "UFS",
182294060Ssmh	.probe = probe,
183294060Ssmh	.load = load,
184294060Ssmh	.status = status
185294060Ssmh};
186