Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/stand/common/metadata.c
34677 views
1
/*-
2
* Copyright (c) 1998 Michael Smith <[email protected]>
3
* All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
*
14
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
* SUCH DAMAGE.
25
*
26
* from: FreeBSD: src/sys/boot/sparc64/loader/metadata.c,v 1.6
27
*/
28
29
#include <stand.h>
30
#include <sys/param.h>
31
#include <sys/linker.h>
32
#include <sys/boot.h>
33
#include <sys/reboot.h>
34
#if defined(LOADER_FDT_SUPPORT)
35
#include <fdt_platform.h>
36
#endif
37
38
#ifdef __arm__
39
#include <machine/elf.h>
40
#endif
41
#include <machine/metadata.h>
42
43
#include "bootstrap.h"
44
#include "modinfo.h"
45
46
#ifdef LOADER_GELI_SUPPORT
47
#include "geliboot.h"
48
#endif
49
50
static int
51
md_getboothowto(char *kargs)
52
{
53
int howto;
54
55
/* Parse kargs */
56
howto = boot_parse_cmdline(kargs);
57
howto |= boot_env_to_howto();
58
if (!strcmp(getenv("console"), "comconsole"))
59
howto |= RB_SERIAL;
60
if (!strcmp(getenv("console"), "nullconsole"))
61
howto |= RB_MUTE;
62
return(howto);
63
}
64
65
/*
66
* Load the information expected by a kernel.
67
*
68
* - The 'boothowto' argument is constructed
69
* - The 'bootdev' argument is constructed
70
* - The kernel environment is copied into kernel space.
71
* - Module metadata are formatted and placed in kernel space.
72
*/
73
static int
74
md_load_dual(char *args, vm_offset_t *modulep, vm_offset_t *dtb, int kern64)
75
{
76
struct preloaded_file *kfp;
77
struct preloaded_file *xp;
78
struct file_metadata *md;
79
vm_offset_t kernend;
80
vm_offset_t addr;
81
vm_offset_t envp;
82
#if defined(LOADER_FDT_SUPPORT)
83
vm_offset_t fdtp;
84
#endif
85
vm_offset_t size;
86
uint64_t scratch64;
87
char *rootdevname;
88
int howto;
89
#ifdef __arm__
90
vm_offset_t vaddr;
91
int i;
92
93
/*
94
* These metadata addreses must be converted for kernel after
95
* relocation.
96
*/
97
uint32_t mdt[] = {
98
MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND,
99
MODINFOMD_ENVP,
100
#if defined(LOADER_FDT_SUPPORT)
101
MODINFOMD_DTBP
102
#endif
103
};
104
#endif
105
106
howto = md_getboothowto(args);
107
108
/*
109
* Allow the environment variable 'rootdev' to override the supplied
110
* device. This should perhaps go to MI code and/or have $rootdev
111
* tested/set by MI code before launching the kernel.
112
*/
113
rootdevname = getenv("rootdev");
114
if (rootdevname == NULL || *rootdevname == '\0')
115
rootdevname = getenv("currdev");
116
/* Try reading the /etc/fstab file to select the root device */
117
getrootmount(rootdevname);
118
119
/* Find the last module in the chain */
120
addr = 0;
121
for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
122
if (addr < (xp->f_addr + xp->f_size))
123
addr = xp->f_addr + xp->f_size;
124
}
125
/* Pad to a page boundary */
126
addr = md_align(addr);
127
128
/* Copy our environment */
129
envp = addr;
130
addr = md_copyenv(addr);
131
132
/* Pad to a page boundary */
133
addr = md_align(addr);
134
135
#if defined(LOADER_FDT_SUPPORT)
136
/* Copy out FDT */
137
fdtp = 0;
138
#if defined(__powerpc__)
139
if (getenv("usefdt") != NULL)
140
#endif
141
{
142
size = fdt_copy(addr);
143
fdtp = addr;
144
addr = md_align(addr + size);
145
}
146
#endif
147
148
kernend = 0;
149
kfp = file_findfile(NULL, md_kerntype);
150
if (kfp == NULL)
151
panic("can't find kernel file");
152
file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
153
if (kern64) {
154
scratch64 = envp;
155
file_addmetadata(kfp, MODINFOMD_ENVP, sizeof scratch64, &scratch64);
156
#if defined(LOADER_FDT_SUPPORT)
157
if (fdtp != 0) {
158
scratch64 = fdtp;
159
file_addmetadata(kfp, MODINFOMD_DTBP, sizeof scratch64, &scratch64);
160
}
161
#endif
162
scratch64 = kernend;
163
file_addmetadata(kfp, MODINFOMD_KERNEND,
164
sizeof scratch64, &scratch64);
165
} else {
166
file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
167
#if defined(LOADER_FDT_SUPPORT)
168
if (fdtp != 0)
169
file_addmetadata(kfp, MODINFOMD_DTBP, sizeof fdtp, &fdtp);
170
#endif
171
file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
172
}
173
#ifdef LOADER_GELI_SUPPORT
174
geli_export_key_metadata(kfp);
175
#endif
176
177
*modulep = addr;
178
size = md_copymodules(0, kern64);
179
kernend = md_align(addr + size);
180
181
md = file_findmetadata(kfp, MODINFOMD_KERNEND);
182
if (kern64) {
183
scratch64 = kernend;
184
bcopy(&scratch64, md->md_data, sizeof scratch64);
185
} else {
186
bcopy(&kernend, md->md_data, sizeof kernend);
187
}
188
189
#ifdef __arm__
190
/* Convert addresses to the final VA */
191
*modulep -= __elfN(relocation_offset);
192
193
/* Do relocation fixup on metadata of each module. */
194
for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
195
for (i = 0; i < nitems(mdt); i++) {
196
md = file_findmetadata(xp, mdt[i]);
197
if (md) {
198
bcopy(md->md_data, &vaddr, sizeof vaddr);
199
vaddr -= __elfN(relocation_offset);
200
bcopy(&vaddr, md->md_data, sizeof vaddr);
201
}
202
}
203
}
204
#endif
205
206
(void)md_copymodules(addr, kern64);
207
#if defined(LOADER_FDT_SUPPORT)
208
if (dtb != NULL)
209
*dtb = fdtp;
210
#endif
211
212
return(0);
213
}
214
215
int
216
md_load(char *args, vm_offset_t *modulep, vm_offset_t *dtb)
217
{
218
return (md_load_dual(args, modulep, dtb, 0));
219
}
220
221
#if defined(__powerpc__)
222
int
223
md_load64(char *args, vm_offset_t *modulep, vm_offset_t *dtb)
224
{
225
return (md_load_dual(args, modulep, dtb, 1));
226
}
227
#endif
228
229