summaryrefslogtreecommitdiff
path: root/debian/patches/50_initrd.patch
blob: c43875718716a5097c9e8ebd25bb13da77748fe9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
Jérémie Koenig <jk@jk.fr.eu.org>
Add ramdisk support for d-i.

--- a/Makefrag.am
+++ b/Makefrag.am
@@ -306,6 +306,8 @@ libkernel_a_SOURCES += \
 	device/net_io.c \
 	device/net_io.h \
 	device/param.h \
+	device/ramdisk.c \
+	device/ramdisk.h \
 	device/subrs.c \
 	device/subrs.h \
 	device/tty.h
--- a/i386/i386at/conf.c
+++ b/i386/i386at/conf.c
@@ -31,6 +31,7 @@
 #include <device/conf.h>
 #include <kern/mach_clock.h>
 #include <i386at/model_dep.h>
+#include <device/ramdisk.h>
 
 #define	timename		"time"
 
@@ -129,6 +130,8 @@ struct dev_ops	dev_name_list[] =
 	  nodev },
 #endif	/* MACH_HYP */
 
+	RAMDISK_DEV_OPS,
+
 #ifdef	MACH_KMSG
         { kmsgname,     kmsgopen,       kmsgclose,       kmsgread,
           nodev,        kmsggetstat,    nodev,           nomap,
--- a/kern/boot_script.c
+++ b/kern/boot_script.c
@@ -88,12 +88,20 @@ prompt_resume_task (struct cmd *cmd, lon
   return boot_script_prompt_task_resume (cmd);
 }
 
+/* Create an initial ramdisk */
+static int
+ramdisk_create (struct cmd *cmd, long *val)
+{
+  return boot_script_ramdisk_create (cmd, (char **) val);
+}
+
 /* List of builtin symbols.  */
 static struct sym builtin_symbols[] =
 {
   { "task-create", VAL_FUNC, (long) create_task, VAL_TASK, 0 },
   { "task-resume", VAL_FUNC, (long) resume_task, VAL_NONE, 1 },
   { "prompt-task-resume", VAL_FUNC, (long) prompt_resume_task, VAL_NONE, 1 },
+  { "ramdisk-create", VAL_FUNC, (long) ramdisk_create, VAL_STR, 0 },
 };
 #define NUM_BUILTIN (sizeof (builtin_symbols) / sizeof (builtin_symbols[0]))
 
--- a/kern/bootstrap.c
+++ b/kern/bootstrap.c
@@ -51,6 +51,7 @@
 #include <vm/vm_user.h>
 #include <vm/pmap.h>
 #include <device/device_port.h>
+#include <device/ramdisk.h>
 
 #if	MACH_KDB
 #include <machine/db_machdep.h>
@@ -796,6 +797,23 @@ boot_script_free (void *ptr, unsigned in
 }
 
 int
+boot_script_ramdisk_create (struct cmd *cmd, char **name)
+{
+  struct multiboot_module *mod = cmd->hook;
+  vm_size_t size = mod->mod_end - mod->mod_start;
+  kern_return_t rc;
+  int no;
+
+  rc = ramdisk_create (size, (void *) phystokv (mod->mod_start), &no);
+  if (rc != KERN_SUCCESS)
+    return BOOT_SCRIPT_MACH_ERROR;
+
+  *name = boot_script_malloc (RAMDISK_NAMESZ);
+  sprintf(*name, RAMDISK_NAME "%d", no);
+  return 0;
+}
+
+int
 boot_script_task_create (struct cmd *cmd)
 {
   kern_return_t rc = task_create(TASK_NULL, FALSE, &cmd->task);
--- /dev/null
+++ b/device/ramdisk.c
@@ -0,0 +1,160 @@
+#include <mach/vm_param.h>
+#include <machine/vm_param.h>
+#include <vm/vm_kern.h>
+#include <vm/vm_user.h>
+#include <device/device_types.h>
+#include <device/ds_routines.h>
+#include <device/conf.h>
+#include <device/ramdisk.h>
+#include <kern/printf.h>
+#include <string.h>
+
+static struct ramdisk {
+	void *data;
+	vm_size_t size;
+} ramdisk[RAMDISK_MAX];
+
+static int ramdisk_num = 0;
+
+/* Initial ramdisks are created from the boot scripts */
+int ramdisk_create(vm_size_t size, const void *initdata, int *out_no)
+{
+	struct ramdisk *rd = &ramdisk[ramdisk_num];
+	int err;
+
+	if(ramdisk_num >= RAMDISK_MAX)
+		return -1;
+
+	/* allocate the memory */
+	rd->size = round_page(size);
+	err = kmem_alloc(kernel_map, (vm_offset_t *) &rd->data, rd->size);
+	if(err != KERN_SUCCESS)
+		return err;
+
+	/* initialize */
+	if(initdata)
+		memcpy(rd->data, initdata, rd->size);
+	else
+		memset(rd->data, 0, rd->size);
+
+	/* report */
+	if(out_no) *out_no = ramdisk_num;
+	printf("%s%d: %lu bytes @%p\n", RAMDISK_NAME, ramdisk_num,
+			(unsigned long) rd->size, rd->data);
+
+	ramdisk_num++;
+	return KERN_SUCCESS;
+}
+
+/* On d_open() we just check whether the ramdisk exists */
+int ramdisk_open(int dev, int mode, io_req_t ior)
+{
+	return (dev < ramdisk_num) ? D_SUCCESS : D_NO_SUCH_DEVICE;
+}
+
+/* d_getstat() is used to query the device characteristics */
+int ramdisk_getstat(int dev, dev_flavor_t flavor, dev_status_t status,
+		mach_msg_type_number_t *status_count)
+{
+	switch(flavor) {
+		case DEV_GET_SIZE:
+			status[DEV_GET_SIZE_DEVICE_SIZE] = ramdisk[dev].size;
+			status[DEV_GET_SIZE_RECORD_SIZE] = RAMDISK_BLOCKSZ;
+			*status_count = DEV_GET_SIZE_COUNT;
+			return D_SUCCESS;
+
+		case DEV_GET_RECORDS:
+			status[DEV_GET_RECORDS_DEVICE_RECORDS]
+					= ramdisk[dev].size / RAMDISK_BLOCKSZ;
+			status[DEV_GET_RECORDS_RECORD_SIZE] = RAMDISK_BLOCKSZ;
+			*status_count = DEV_GET_RECORDS_COUNT;
+			return D_SUCCESS;
+	}
+	return D_INVALID_OPERATION;
+}
+
+/* TODO: implement freeramdisk with setstat() ? */
+
+/* Check the given io request and compute a pointer to the ramdisk data and the
+ * amount to be handled. */
+static int ramdisk_ioreq(int dev, io_req_t ior, void **data, int *amt)
+{
+	vm_offset_t ofs = ior->io_recnum * RAMDISK_BLOCKSZ;
+	if(ofs >= ramdisk[dev].size)
+		return D_INVALID_RECNUM;
+
+	*data = (char*) ramdisk[dev].data + ofs;
+	*amt = ior->io_count;
+	if(ofs + *amt > ramdisk[dev].size)
+		*amt = ramdisk[dev].size - ofs;
+
+	return KERN_SUCCESS;
+}
+
+/* Copy data from a vm_map_copy by mapping it temporarily. */
+static int mem_map_cpy(void *dst, vm_map_copy_t src, int amt)
+{
+	vm_offset_t srcaddr;
+	int err;
+
+	err = vm_map_copyout(device_io_map, &srcaddr, src);
+	if (err != KERN_SUCCESS)
+		return err;
+
+	memcpy(dst, (void *) srcaddr, amt);
+	vm_deallocate(device_io_map, srcaddr, amt);
+	return KERN_SUCCESS;
+}
+
+int ramdisk_read(int dev, io_req_t ior)
+{
+	void *data;
+	int amt, err;
+
+	err = ramdisk_ioreq(dev, ior, &data, &amt);
+	if(err != KERN_SUCCESS)
+		return err;
+
+	err = device_read_alloc (ior, ior->io_count);
+	if (err != KERN_SUCCESS)
+		return err;
+
+	memcpy(ior->io_data, data, amt);
+	ior->io_residual = ior->io_count - amt;
+
+	return D_SUCCESS;
+}
+
+int ramdisk_write(int dev, io_req_t ior)
+{
+	void *data;
+	int amt, err;
+
+	err = ramdisk_ioreq(dev, ior, &data, &amt);
+	if(err != KERN_SUCCESS)
+		return err;
+
+	if (!(ior->io_op & IO_INBAND)) {
+		/* Out-of-band data is transmitted as a vm_map_copy */
+		err = mem_map_cpy(data, (vm_map_copy_t) ior->io_data, amt);
+		if(err != KERN_SUCCESS)
+			return err;
+	} else {
+		/* In-band data can be accessed directly */
+		memcpy(data, ior->io_data, amt);
+	}
+
+	ior->io_residual = ior->io_count - amt;
+	return D_SUCCESS;
+}
+
+vm_offset_t ramdisk_mmap(int dev, vm_offset_t off, vm_prot_t prot)
+{
+	if(dev >= ramdisk_num)
+		return -1;
+	if(off >= ramdisk[dev].size)
+		return -1;
+
+	return pmap_phys_to_frame(kvtophys((vm_offset_t) ramdisk[dev].data + off));
+}
+
--- /dev/null
+++ b/device/ramdisk.h
@@ -0,0 +1,47 @@
+#ifndef _KERN_RAMDISK_H_
+#define _KERN_RAMDISK_H_
+
+#include <vm/pmap.h>
+#include <device/io_req.h>
+#include <device/conf.h>
+
+/* Maximum number of ramdisk devices */
+#define RAMDISK_MAX 4
+
+/* The block size used (userspace requires 512) */
+#define RAMDISK_BLOCKSZ 512
+
+/* Name associated to the ramdisk major */
+#define RAMDISK_NAME "rd"
+#define RAMDISK_NAMESZ (sizeof RAMDISK_NAME + sizeof (int) * 3 + 1)
+
+/* Create a new ramdisk of the given size. On success, if out_no and/or out_ptr
+ * are not NULL, the device number and pointer to the ramdisk's data are stored
+ * there. Returns D_SUCCESS or D_NO_MEMORY.  */
+int ramdisk_create(vm_size_t size, const void *initdata, int *out_no);
+
+/* Device operations */
+int ramdisk_open(int, int, io_req_t);
+int ramdisk_getstat(int, dev_flavor_t, dev_status_t, mach_msg_type_number_t *);
+int ramdisk_read(int, io_req_t);
+int ramdisk_write(int, io_req_t);
+vm_offset_t ramdisk_mmap(int, vm_offset_t, vm_prot_t);
+
+/* dev_ops initializer to be used from <machine>/conf.c */
+#define RAMDISK_DEV_OPS { \
+		.d_name = RAMDISK_NAME, \
+		.d_open = ramdisk_open, \
+		.d_close = nulldev, \
+		.d_read = ramdisk_read, \
+		.d_write = ramdisk_write, \
+		.d_getstat = ramdisk_getstat, \
+		.d_setstat = nodev, \
+		.d_mmap = ramdisk_mmap, \
+		.d_async_in = nodev, \
+		.d_reset = nulldev, \
+		.d_port_death = nulldev, \
+		.d_subdev = 0, \
+		.d_dev_info = nodev, \
+	}
+
+#endif