devopen.c revision 1.4 1 1.4 nonaka /* $NetBSD: devopen.c,v 1.4 2018/04/02 09:44:18 nonaka Exp $ */
2 1.1 nonaka
3 1.1 nonaka /*-
4 1.1 nonaka * Copyright (c) 2005 The NetBSD Foundation, Inc.
5 1.1 nonaka * All rights reserved.
6 1.1 nonaka *
7 1.1 nonaka * This code is derived from software contributed to The NetBSD Foundation
8 1.1 nonaka * by Bang Jun-Young.
9 1.1 nonaka *
10 1.1 nonaka * Redistribution and use in source and binary forms, with or without
11 1.1 nonaka * modification, are permitted provided that the following conditions
12 1.1 nonaka * are met:
13 1.1 nonaka * 1. Redistributions of source code must retain the above copyright
14 1.1 nonaka * notice, this list of conditions and the following disclaimer.
15 1.1 nonaka * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 nonaka * notice, this list of conditions and the following disclaimer in the
17 1.1 nonaka * documentation and/or other materials provided with the distribution.
18 1.1 nonaka *
19 1.1 nonaka * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 nonaka * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 nonaka * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 nonaka * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 nonaka * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 nonaka * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 nonaka * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 nonaka * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 nonaka * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 nonaka * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 nonaka * POSSIBILITY OF SUCH DAMAGE.
30 1.1 nonaka */
31 1.1 nonaka
32 1.1 nonaka /*
33 1.1 nonaka * Copyright (c) 1996, 1997
34 1.1 nonaka * Matthias Drochner. All rights reserved.
35 1.1 nonaka *
36 1.1 nonaka * Redistribution and use in source and binary forms, with or without
37 1.1 nonaka * modification, are permitted provided that the following conditions
38 1.1 nonaka * are met:
39 1.1 nonaka * 1. Redistributions of source code must retain the above copyright
40 1.1 nonaka * notice, this list of conditions and the following disclaimer.
41 1.1 nonaka * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 nonaka * notice, this list of conditions and the following disclaimer in the
43 1.1 nonaka * documentation and/or other materials provided with the distribution.
44 1.1 nonaka *
45 1.1 nonaka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46 1.1 nonaka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 1.1 nonaka * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48 1.1 nonaka * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49 1.1 nonaka * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50 1.1 nonaka * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51 1.1 nonaka * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52 1.1 nonaka * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53 1.1 nonaka * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54 1.1 nonaka * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 1.1 nonaka */
56 1.1 nonaka
57 1.1 nonaka #include "efiboot.h"
58 1.1 nonaka
59 1.1 nonaka #include <biosdisk.h>
60 1.1 nonaka #include "devopen.h"
61 1.1 nonaka #include <bootinfo.h>
62 1.4 nonaka #include "efidisk.h"
63 1.1 nonaka
64 1.1 nonaka static int
65 1.1 nonaka dev2bios(char *devname, int unit, int *biosdev)
66 1.1 nonaka {
67 1.1 nonaka
68 1.1 nonaka if (strcmp(devname, "hd") == 0)
69 1.1 nonaka *biosdev = 0x80 + unit;
70 1.3 nonaka else if (strcmp(devname, "cd") == 0)
71 1.2 nonaka *biosdev = 0x80 + get_harddrives() + unit;
72 1.1 nonaka else
73 1.1 nonaka return ENXIO;
74 1.2 nonaka
75 1.1 nonaka return 0;
76 1.1 nonaka }
77 1.1 nonaka
78 1.1 nonaka void
79 1.1 nonaka bios2dev(int biosdev, daddr_t sector, char **devname, int *unit, int *partition)
80 1.1 nonaka {
81 1.1 nonaka
82 1.1 nonaka *unit = biosdev & 0x7f;
83 1.2 nonaka
84 1.2 nonaka if (biosdev >= 0x80 + get_harddrives()) {
85 1.2 nonaka *devname = "cd";
86 1.2 nonaka *unit -= get_harddrives();
87 1.2 nonaka } else
88 1.2 nonaka *devname = "hd";
89 1.2 nonaka
90 1.1 nonaka *partition = biosdisk_findpartition(biosdev, sector);
91 1.1 nonaka }
92 1.1 nonaka
93 1.1 nonaka struct btinfo_bootpath bibp;
94 1.1 nonaka extern bool kernel_loaded;
95 1.1 nonaka
96 1.1 nonaka /*
97 1.1 nonaka * Open the EFI disk device
98 1.1 nonaka */
99 1.1 nonaka int
100 1.1 nonaka devopen(struct open_file *f, const char *fname, char **file)
101 1.1 nonaka {
102 1.1 nonaka char *fsname, *devname;
103 1.1 nonaka int unit, partition;
104 1.1 nonaka int biosdev;
105 1.1 nonaka int error;
106 1.1 nonaka
107 1.4 nonaka error = parsebootfile(fname, &fsname, &devname, &unit, &partition,
108 1.4 nonaka (const char **) file);
109 1.4 nonaka if (error)
110 1.4 nonaka return error;
111 1.4 nonaka
112 1.4 nonaka if (strcmp(devname, "esp") == 0) {
113 1.4 nonaka bios2dev(boot_biosdev, boot_biossector, &devname, &unit,
114 1.4 nonaka &partition);
115 1.4 nonaka if (efidisk_get_efi_system_partition(boot_biosdev, &partition))
116 1.4 nonaka return ENXIO;
117 1.4 nonaka }
118 1.4 nonaka
119 1.4 nonaka error = dev2bios(devname, unit, &biosdev);
120 1.4 nonaka if (error)
121 1.1 nonaka return error;
122 1.1 nonaka
123 1.1 nonaka f->f_dev = &devsw[0]; /* must be biosdisk */
124 1.1 nonaka
125 1.1 nonaka if (!kernel_loaded) {
126 1.1 nonaka strncpy(bibp.bootpath, *file, sizeof(bibp.bootpath));
127 1.1 nonaka BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
128 1.1 nonaka }
129 1.1 nonaka
130 1.1 nonaka return biosdisk_open(f, biosdev, partition);
131 1.1 nonaka }
132