summaryrefslogtreecommitdiffstats
path: root/src/arch/x86/image/initrd.c
blob: 80c197417145a3013080f3a5acba00b2a5da8119 (plain) (blame)
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
304
/*
 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <errno.h>
#include <initrd.h>
#include <ipxe/image.h>
#include <ipxe/uaccess.h>
#include <ipxe/init.h>
#include <ipxe/memblock.h>

/** @file
 *
 * Initial ramdisk (initrd) reshuffling
 *
 */

/** Maximum address available for initrd */
userptr_t initrd_top;

/** Minimum address available for initrd */
userptr_t initrd_bottom;

/**
 * Squash initrds as high as possible in memory
 *
 * @v top		Highest possible address
 * @ret used		Lowest address used by initrds
 */
static userptr_t initrd_squash_high ( userptr_t top ) {
	userptr_t current = top;
	struct image *initrd;
	struct image *highest;
	size_t len;

	/* Squash up any initrds already within or below the region */
	while ( 1 ) {

		/* Find the highest image not yet in its final position */
		highest = NULL;
		for_each_image ( initrd ) {
			if ( ( userptr_sub ( initrd->data, current ) < 0 ) &&
			     ( ( highest == NULL ) ||
			       ( userptr_sub ( initrd->data,
					       highest->data ) > 0 ) ) ) {
				highest = initrd;
			}
		}
		if ( ! highest )
			break;

		/* Move this image to its final position */
		len = ( ( highest->len + INITRD_ALIGN - 1 ) &
			~( INITRD_ALIGN - 1 ) );
		current = userptr_sub ( current, len );
		DBGC ( &images, "INITRD squashing %s [%#08lx,%#08lx)->"
		       "[%#08lx,%#08lx)\n", highest->name,
		       user_to_phys ( highest->data, 0 ),
		       user_to_phys ( highest->data, highest->len ),
		       user_to_phys ( current, 0 ),
		       user_to_phys ( current, highest->len ) );
		memmove_user ( current, 0, highest->data, 0, highest->len );
		highest->data = current;
	}

	/* Copy any remaining initrds (e.g. embedded images) to the region */
	for_each_image ( initrd ) {
		if ( userptr_sub ( initrd->data, top ) >= 0 ) {
			len = ( ( initrd->len + INITRD_ALIGN - 1 ) &
				~( INITRD_ALIGN - 1 ) );
			current = userptr_sub ( current, len );
			DBGC ( &images, "INITRD copying %s [%#08lx,%#08lx)->"
			       "[%#08lx,%#08lx)\n", initrd->name,
			       user_to_phys ( initrd->data, 0 ),
			       user_to_phys ( initrd->data, initrd->len ),
			       user_to_phys ( current, 0 ),
			       user_to_phys ( current, initrd->len ) );
			memcpy_user ( current, 0, initrd->data, 0,
				      initrd->len );
			initrd->data = current;
		}
	}

	return current;
}

/**
 * Swap position of two adjacent initrds
 *
 * @v low		Lower initrd
 * @v high		Higher initrd
 * @v free		Free space
 * @v free_len		Length of free space
 */
static void initrd_swap ( struct image *low, struct image *high,
			  userptr_t free, size_t free_len ) {
	size_t len = 0;
	size_t frag_len;
	size_t new_len;

	DBGC ( &images, "INITRD swapping %s [%#08lx,%#08lx)<->[%#08lx,%#08lx) "
	       "%s\n", low->name, user_to_phys ( low->data, 0 ),
	       user_to_phys ( low->data, low->len ),
	       user_to_phys ( high->data, 0 ),
	       user_to_phys ( high->data, high->len ), high->name );

	/* Round down length of free space */
	free_len &= ~( INITRD_ALIGN - 1 );
	assert ( free_len > 0 );

	/* Swap image data */
	while ( len < high->len ) {

		/* Calculate maximum fragment length */
		frag_len = ( high->len - len );
		if ( frag_len > free_len )
			frag_len = free_len;
		new_len = ( ( len + frag_len + INITRD_ALIGN - 1 ) &
			    ~( INITRD_ALIGN - 1 ) );

		/* Swap fragments */
		memcpy_user ( free, 0, high->data, len, frag_len );
		memmove_user ( low->data, new_len, low->data, len, low->len );
		memcpy_user ( low->data, len, free, 0, frag_len );
		len = new_len;
	}

	/* Adjust data pointers */
	high->data = low->data;
	low->data = userptr_add ( low->data, len );
}

/**
 * Swap position of any two adjacent initrds not currently in the correct order
 *
 * @v free		Free space
 * @v free_len		Length of free space
 * @ret swapped		A pair of initrds was swapped
 */
static int initrd_swap_any ( userptr_t free, size_t free_len ) {
	struct image *low;
	struct image *high;
	size_t padded_len;
	userptr_t adjacent;

	/* Find any pair of initrds that can be swapped */
	for_each_image ( low ) {

		/* Calculate location of adjacent image (if any) */
		padded_len = ( ( low->len + INITRD_ALIGN - 1 ) &
			       ~( INITRD_ALIGN - 1 ) );
		adjacent = userptr_add ( low->data, padded_len );

		/* Search for adjacent image */
		for_each_image ( high ) {

			/* If we have found the adjacent image, swap and exit */
			if ( high->data == adjacent ) {
				initrd_swap ( low, high, free, free_len );
				return 1;
			}

			/* Stop search if all remaining potential
			 * adjacent images are already in the correct
			 * order.
			 */
			if ( high == low )
				break;
		}
	}

	/* Nothing swapped */
	return 0;
}

/**
 * Dump initrd locations (for debug)
 *
 */
static void initrd_dump ( void ) {
	struct image *initrd;

	/* Do nothing unless debugging is enabled */
	if ( ! DBG_LOG )
		return;

	/* Dump initrd locations */
	for_each_image ( initrd ) {
		DBGC ( &images, "INITRD %s at [%#08lx,%#08lx)\n",
		       initrd->name, user_to_phys ( initrd->data, 0 ),
		       user_to_phys ( initrd->data, initrd->len ) );
		DBGC2_MD5A ( &images, user_to_phys ( initrd->data, 0 ),
			     user_to_virt ( initrd->data, 0 ), initrd->len );
	}
}

/**
 * Reshuffle initrds into desired order at top of memory
 *
 * @v bottom		Lowest address available for initrds
 *
 * After this function returns, the initrds have been rearranged in
 * memory and the external heap structures will have been corrupted.
 * Reshuffling must therefore take place immediately prior to jumping
 * to the loaded OS kernel; no further execution within iPXE is
 * permitted.
 */
void initrd_reshuffle ( userptr_t bottom ) {
	userptr_t top;
	userptr_t used;
	userptr_t free;
	size_t free_len;

	/* Calculate limits of available space for initrds */
	top = initrd_top;
	if ( userptr_sub ( initrd_bottom, bottom ) > 0 )
		bottom = initrd_bottom;

	/* Debug */
	DBGC ( &images, "INITRD region [%#08lx,%#08lx)\n",
	       user_to_phys ( bottom, 0 ), user_to_phys ( top, 0 ) );
	initrd_dump();

	/* Squash initrds as high as possible in memory */
	used = initrd_squash_high ( top );

	/* Calculate available free space */
	free = bottom;
	free_len = userptr_sub ( used, free );

	/* Bubble-sort initrds into desired order */
	while ( initrd_swap_any ( free, free_len ) ) {}

	/* Debug */
	initrd_dump();
}

/**
 * Check that there is enough space to reshuffle initrds
 *
 * @v len		Total length of initrds (including padding)
 * @v bottom		Lowest address available for initrds
 * @ret rc		Return status code
 */
int initrd_reshuffle_check ( size_t len, userptr_t bottom ) {
	userptr_t top;
	size_t available;

	/* Calculate limits of available space for initrds */
	top = initrd_top;
	if ( userptr_sub ( initrd_bottom, bottom ) > 0 )
		bottom = initrd_bottom;
	available = userptr_sub ( top, bottom );

	/* Allow for a sensible minimum amount of free space */
	len += INITRD_MIN_FREE_LEN;

	/* Check for available space */
	return ( ( len < available ) ? 0 : -ENOBUFS );
}

/**
 * initrd startup function
 *
 */
static void initrd_startup ( void ) {
	size_t len;

	/* Record largest memory block available.  Do this after any
	 * allocations made during driver startup (e.g. large host
	 * memory blocks for Infiniband devices, which may still be in
	 * use at the time of rearranging if a SAN device is hooked)
	 * but before any allocations for downloaded images (which we
	 * can safely reuse when rearranging).
	 */
	len = largest_memblock ( &initrd_bottom );
	initrd_top = userptr_add ( initrd_bottom, len );
}

/** initrd startup function */
struct startup_fn startup_initrd __startup_fn ( STARTUP_LATE ) = {
	.startup = initrd_startup,
};