ufs_module.c revision 294997
1/*-
2 * Copyright (c) 1998 Robert Nordier
3 * All rights reserved.
4 * Copyright (c) 2001 Robert Drehmel
5 * All rights reserved.
6 * Copyright (c) 2014 Nathan Whitehorn
7 * All rights reserved.
8 * Copyright (c) 2015 Eric McCorkle
9 * All rights reverved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $FreeBSD: stable/10/sys/boot/efi/boot1/ufs_module.c 294997 2016-01-28 16:52:02Z smh $
33 */
34
35#include <stdarg.h>
36#include <stdbool.h>
37#include <sys/cdefs.h>
38#include <sys/param.h>
39#include <efi.h>
40
41#include "boot_module.h"
42
43static dev_info_t *devinfo;
44static dev_info_t *devices;
45
46static int
47dskread(void *buf, u_int64_t lba, int nblk)
48{
49	int size;
50	EFI_STATUS status;
51
52	lba = lba / (devinfo->dev->Media->BlockSize / DEV_BSIZE);
53	size = nblk * DEV_BSIZE;
54
55	status = devinfo->dev->ReadBlocks(devinfo->dev,
56	    devinfo->dev->Media->MediaId, lba, size, buf);
57
58	if (status != EFI_SUCCESS) {
59		DPRINTF("dskread: failed dev: %p, id: %u, lba: %lu, size: %d, "
60		    "status: %lu\n", devinfo->dev,
61		    devinfo->dev->Media->MediaId, lba, size,
62		    EFI_ERROR_CODE(status));
63		return (-1);
64	}
65
66	return (0);
67}
68
69#include "ufsread.c"
70
71static struct dmadat __dmadat;
72
73static int
74init_dev(dev_info_t* dev)
75{
76
77	devinfo = dev;
78	dmadat = &__dmadat;
79
80	return fsread(0, NULL, 0);
81}
82
83static EFI_STATUS
84probe(dev_info_t* dev)
85{
86
87	if (init_dev(dev) < 0)
88		return (EFI_UNSUPPORTED);
89
90	add_device(&devices, dev);
91
92	return (EFI_SUCCESS);
93}
94
95static EFI_STATUS
96try_load(dev_info_t *dev, const char *loader_path, void **bufp, size_t *bufsize)
97{
98	ufs_ino_t ino;
99	EFI_STATUS status;
100	size_t size;
101	ssize_t read;
102	void *buf;
103
104	if (init_dev(dev) < 0)
105		return (EFI_UNSUPPORTED);
106
107	if ((ino = lookup(loader_path)) == 0)
108		return (EFI_NOT_FOUND);
109
110	if (fsread_size(ino, NULL, 0, &size) < 0 || size <= 0) {
111		printf("Failed to read size of '%s' ino: %d\n", loader_path,
112		    ino);
113		return (EFI_INVALID_PARAMETER);
114	}
115
116	if ((status = bs->AllocatePool(EfiLoaderData, size, &buf)) !=
117	    EFI_SUCCESS) {
118		printf("Failed to allocate read buffer (%lu)\n",
119		    EFI_ERROR_CODE(status));
120		return (status);
121	}
122
123	read = fsread(ino, buf, size);
124	if ((size_t)read != size) {
125		printf("Failed to read '%s' (%zd != %zu)\n", loader_path, read,
126		    size);
127		(void)bs->FreePool(buf);
128		return (EFI_INVALID_PARAMETER);
129	}
130
131	*bufp = buf;
132	*bufsize = size;
133
134	return (EFI_SUCCESS);
135}
136
137static EFI_STATUS
138load(const char *loader_path, dev_info_t **devinfop, void **buf,
139    size_t *bufsize)
140{
141	dev_info_t *dev;
142	EFI_STATUS status;
143
144	for (dev = devices; dev != NULL; dev = dev->next) {
145		status = try_load(dev, loader_path, buf, bufsize);
146		if (status == EFI_SUCCESS) {
147			*devinfop = dev;
148			return (EFI_SUCCESS);
149		} else if (status != EFI_NOT_FOUND) {
150			return (status);
151		}
152	}
153
154	return (EFI_NOT_FOUND);
155}
156
157static void
158status()
159{
160	int i;
161	dev_info_t *dev;
162
163	for (dev = devices, i = 0; dev != NULL; dev = dev->next, i++)
164		;
165
166	printf("%s found ", ufs_module.name);
167	switch (i) {
168	case 0:
169		printf("no partitions\n");
170		break;
171	case 1:
172		printf("%d partition\n", i);
173		break;
174	default:
175		printf("%d partitions\n", i);
176	}
177}
178
179const boot_module_t ufs_module =
180{
181	.name = "UFS",
182	.probe = probe,
183	.load = load,
184	.status = status
185};
186