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: releng/10.3/sys/boot/efi/boot1/ufs_module.c 295550 2016-02-11 22:33:47Z 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
71294997Ssmhstatic struct dmadat __dmadat;
72294997Ssmh
73294997Ssmhstatic int
74294997Ssmhinit_dev(dev_info_t* dev)
75294060Ssmh{
76294060Ssmh
77294997Ssmh	devinfo = dev;
78294997Ssmh	dmadat = &__dmadat;
79294060Ssmh
80294997Ssmh	return fsread(0, NULL, 0);
81294060Ssmh}
82294060Ssmh
83294060Ssmhstatic EFI_STATUS
84294060Ssmhprobe(dev_info_t* dev)
85294060Ssmh{
86294060Ssmh
87294997Ssmh	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
96295550Ssmhload(const char *filepath, dev_info_t *dev, 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
104295550Ssmh	DPRINTF("Loading '%s' from %s\n", filepath, devpath_str(dev->devpath));
105295550Ssmh
106295550Ssmh	if (init_dev(dev) < 0) {
107295550Ssmh		DPRINTF("Failed to init device\n");
108294997Ssmh		return (EFI_UNSUPPORTED);
109295550Ssmh	}
110294997Ssmh
111295550Ssmh	if ((ino = lookup(filepath)) == 0) {
112295550Ssmh		DPRINTF("Failed to lookup '%s' (file not found?)\n", filepath);
113294060Ssmh		return (EFI_NOT_FOUND);
114295550Ssmh	}
115294060Ssmh
116294997Ssmh	if (fsread_size(ino, NULL, 0, &size) < 0 || size <= 0) {
117295550Ssmh		printf("Failed to read size of '%s' ino: %d\n", filepath, ino);
118294060Ssmh		return (EFI_INVALID_PARAMETER);
119294060Ssmh	}
120294060Ssmh
121294060Ssmh	if ((status = bs->AllocatePool(EfiLoaderData, size, &buf)) !=
122294060Ssmh	    EFI_SUCCESS) {
123295550Ssmh		printf("Failed to allocate read buffer %zu for '%s' (%lu)\n",
124295550Ssmh		    size, filepath, EFI_ERROR_CODE(status));
125294060Ssmh		return (status);
126294060Ssmh	}
127294060Ssmh
128294060Ssmh	read = fsread(ino, buf, size);
129294060Ssmh	if ((size_t)read != size) {
130295550Ssmh		printf("Failed to read '%s' (%zd != %zu)\n", filepath, read,
131294060Ssmh		    size);
132294060Ssmh		(void)bs->FreePool(buf);
133294060Ssmh		return (EFI_INVALID_PARAMETER);
134294060Ssmh	}
135294060Ssmh
136295550Ssmh	DPRINTF("Load complete\n");
137295550Ssmh
138294060Ssmh	*bufp = buf;
139294060Ssmh	*bufsize = size;
140294060Ssmh
141294060Ssmh	return (EFI_SUCCESS);
142294060Ssmh}
143294060Ssmh
144294060Ssmhstatic void
145294060Ssmhstatus()
146294060Ssmh{
147294060Ssmh	int i;
148294060Ssmh	dev_info_t *dev;
149294060Ssmh
150294060Ssmh	for (dev = devices, i = 0; dev != NULL; dev = dev->next, i++)
151294060Ssmh		;
152294060Ssmh
153294060Ssmh	printf("%s found ", ufs_module.name);
154294060Ssmh	switch (i) {
155294060Ssmh	case 0:
156294060Ssmh		printf("no partitions\n");
157294060Ssmh		break;
158294060Ssmh	case 1:
159294060Ssmh		printf("%d partition\n", i);
160294060Ssmh		break;
161294060Ssmh	default:
162294060Ssmh		printf("%d partitions\n", i);
163294060Ssmh	}
164294060Ssmh}
165294060Ssmh
166295550Ssmhstatic dev_info_t *
167295550Ssmh_devices()
168295550Ssmh{
169295550Ssmh
170295550Ssmh	return (devices);
171295550Ssmh}
172295550Ssmh
173294060Ssmhconst boot_module_t ufs_module =
174294060Ssmh{
175294060Ssmh	.name = "UFS",
176294060Ssmh	.probe = probe,
177294060Ssmh	.load = load,
178295550Ssmh	.status = status,
179295550Ssmh	.devices = _devices
180294060Ssmh};
181