Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*	$NetBSD: alpha.c,v 1.22 2019/05/07 04:35:31 thorpej Exp $	*/

/*-
 * Copyright (c) 2002 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Luke Mewburn of Wasabi Systems.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Copyright (c) 1999 Ross Harvey.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Ross Harvey
 *	for the NetBSD Project.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Copyright (c) 1999 Christopher G. Demetriou.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Christopher G. Demetriou
 *	for the NetBSD Project.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif

#include <sys/cdefs.h>
#if !defined(__lint)
__RCSID("$NetBSD: alpha.c,v 1.22 2019/05/07 04:35:31 thorpej Exp $");
#endif	/* !__lint */

#include <sys/param.h>

#include <assert.h>
#include <err.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "installboot.h"

#define	SUN_DKMAGIC	55998		/* XXX: from <dev/sun/disklabel.h> */

static void	resum(ib_params *, struct alpha_boot_block * const bb,
			uint16_t *bb16);
static void	sun_bootstrap(ib_params *, struct alpha_boot_block * const);
static void	check_sparc(const struct alpha_boot_block * const,
			    const char *);

static int alpha_clearboot(ib_params *);
static int alpha_setboot(ib_params *);

struct ib_mach ib_mach_alpha = {
	.name		=	"alpha",
	.setboot	=	alpha_setboot,
	.clearboot	=	alpha_clearboot,
	.editboot	=	no_editboot,
	.valid_flags	=	IB_STAGE1START | IB_ALPHASUM | IB_APPEND |
				    IB_SUNSUM,
};

static int
alpha_clearboot(ib_params *params)
{
	struct alpha_boot_block	bb;
	uint64_t		cksum;
	ssize_t			rv;

	assert(params != NULL);
	assert(params->fsfd != -1);
	assert(params->filesystem != NULL);
	assert(sizeof(struct alpha_boot_block) == ALPHA_BOOT_BLOCK_BLOCKSIZE);

	if (params->flags & (IB_STAGE1START | IB_APPEND)) {
		warnx("Can't use `-b bno' or `-o append' with `-c'");
		return (0);
	}

	rv = pread(params->fsfd, &bb, sizeof(bb), ALPHA_BOOT_BLOCK_OFFSET);
	if (rv == -1) {
		warn("Reading `%s'", params->filesystem);
		return (0);
	} else if (rv != sizeof(bb)) {
		warnx("Reading `%s': short read", params->filesystem);
		return (0);
	}
	ALPHA_BOOT_BLOCK_CKSUM(&bb, &cksum);
	if (cksum != bb.bb_cksum) {		// XXX check bb_cksum endian?
		warnx(
	    "Old boot block checksum invalid (was %#llx, calculated %#llx)",
		    (unsigned long long)le64toh(bb.bb_cksum),
		    (unsigned long long)le64toh(cksum));
		warnx("Boot block invalid");
		return (0);
	}

	if (params->flags & IB_VERBOSE) {
		printf("Old bootstrap start sector: %llu\n",
		    (unsigned long long)le64toh(bb.bb_secstart));
		printf("Old bootstrap size:         %llu\n",
		    (unsigned long long)le64toh(bb.bb_secsize));
		printf("Old bootstrap checksum:     %#llx\n",
		    (unsigned long long)le64toh(bb.bb_cksum));
	}

	bb.bb_secstart = bb.bb_secsize = bb.bb_flags = 0;

	ALPHA_BOOT_BLOCK_CKSUM(&bb, &bb.bb_cksum);
	if (params->flags & IB_SUNSUM)
		sun_bootstrap(params, &bb);

	printf("New bootstrap start sector: %llu\n",
	    (unsigned long long)le64toh(bb.bb_secstart));
	printf("New bootstrap size:         %llu\n",
	    (unsigned long long)le64toh(bb.bb_secsize));
	printf("New bootstrap checksum:     %#llx\n",
	    (unsigned long long)le64toh(bb.bb_cksum));

	if (params->flags & IB_VERBOSE)
		printf("%slearing boot block\n",
		    (params->flags & IB_NOWRITE) ? "Not c" : "C");
	if (params->flags & IB_NOWRITE)
		return (1);

	rv = pwrite(params->fsfd, &bb, sizeof(bb), ALPHA_BOOT_BLOCK_OFFSET);
	if (rv == -1) {
		warn("Writing `%s'", params->filesystem);
		return (0);
	} else if (rv != sizeof(bb)) {
		warnx("Writing `%s': short write", params->filesystem);
		return (0);
	}

	return (1);
}

static int
alpha_setboot(ib_params *params)
{
	struct alpha_boot_block	bb;
	uint64_t		startblock;
	int			retval;
	char			*bootstrapbuf;
	size_t			bootstrapsize;
	ssize_t			rv;

	assert(params != NULL);
	assert(params->fsfd != -1);
	assert(params->filesystem != NULL);
	assert(params->s1fd != -1);
	assert(params->stage1 != NULL);
	assert(sizeof(struct alpha_boot_block) == ALPHA_BOOT_BLOCK_BLOCKSIZE);

	retval = 0;
	bootstrapbuf = NULL;

	/*
	 * Allocate a buffer, with space to round up the input file
	 * to the next block size boundary, and with space for the boot
	 * block.
	 */
	bootstrapsize = roundup(params->s1stat.st_size,
	    ALPHA_BOOT_BLOCK_BLOCKSIZE);

	bootstrapbuf = malloc(bootstrapsize);
	if (bootstrapbuf == NULL) {
		warn("Allocating %lu bytes", (unsigned long) bootstrapsize);
		goto done;
	}
	memset(bootstrapbuf, 0, bootstrapsize);

	/* read the file into the buffer */
	rv = pread(params->s1fd, bootstrapbuf, params->s1stat.st_size, 0);
	if (rv == -1) {
		warn("Reading `%s'", params->stage1);
		goto done;
	} else if (rv != params->s1stat.st_size) {
		warnx("Reading `%s': short read", params->stage1);
		goto done;
	}

	rv = pread(params->fsfd, &bb, sizeof(bb), ALPHA_BOOT_BLOCK_OFFSET);
	if (rv == -1) {
		warn("Reading `%s'", params->filesystem);
		goto done;
	} else if (rv != sizeof(bb)) {
		warnx("Reading `%s': short read", params->filesystem);
		goto done;
	}

	if (params->flags & IB_SUNSUM)
		check_sparc(&bb, "Initial");

		/* fill in the updated bootstrap fields */
	if (params->flags & IB_APPEND) {
		struct stat	filesyssb;

		if (fstat(params->fsfd, &filesyssb) == -1) {
			warn("Examining `%s'", params->filesystem);
			goto done;
		}
		if (!S_ISREG(filesyssb.st_mode)) {
			warnx(
		    "`%s' must be a regular file to append a bootstrap",
			    params->filesystem);
			goto done;
		}
		startblock = howmany(filesyssb.st_size,
		    ALPHA_BOOT_BLOCK_BLOCKSIZE);
	} else if (params->flags & IB_STAGE1START) {
		startblock = params->s1start;
	} else {
		startblock = ALPHA_BOOT_BLOCK_OFFSET /
		    ALPHA_BOOT_BLOCK_BLOCKSIZE + 1;
	}

	bb.bb_secsize =
	    htole64(howmany(params->s1stat.st_size,
		ALPHA_BOOT_BLOCK_BLOCKSIZE));
	bb.bb_secstart = htole64(startblock);
	bb.bb_flags = 0;

	ALPHA_BOOT_BLOCK_CKSUM(&bb, &bb.bb_cksum);
	if (params->flags & IB_SUNSUM)
		sun_bootstrap(params, &bb);

	if (params->flags & IB_VERBOSE) {
		printf("Bootstrap start sector:  %llu\n",
		    (unsigned long long)startblock);
		printf("Bootstrap sector count:  %llu\n",
		    (unsigned long long)le64toh(bb.bb_secsize));
		printf("New boot block checksum: %#llx\n",
		    (unsigned long long)le64toh(bb.bb_cksum));
		printf("%sriting bootstrap\n",
		    (params->flags & IB_NOWRITE) ? "Not w" : "W");
	}
	if (params->flags & IB_NOWRITE) {
		retval = 1;
		goto done;
	}
	rv = pwrite(params->fsfd, bootstrapbuf, bootstrapsize,
	     startblock * ALPHA_BOOT_BLOCK_BLOCKSIZE);
	if (rv == -1) {
		warn("Writing `%s'", params->filesystem);
		goto done;
	} else if ((size_t)rv != bootstrapsize) {
		warnx("Writing `%s': short write", params->filesystem);
		goto done;
	}

	if (params->flags & IB_VERBOSE)
		printf("Writing boot block\n");
	rv = pwrite(params->fsfd, &bb, sizeof(bb), ALPHA_BOOT_BLOCK_OFFSET);
	if (rv == -1) {
		warn("Writing `%s'", params->filesystem);
		goto done;
	} else if (rv != sizeof(bb)) {
		warnx("Writing `%s': short write", params->filesystem);
		goto done;
	} else {
		retval = 1;
	}

 done:
	if (bootstrapbuf)
		free(bootstrapbuf);
	return (retval);
}


/*
 * The Sun and alpha checksums overlay, and the Sun magic number also
 * overlays the alpha checksum. If you think you are smart: stop here
 * and do exercise one: figure out how to salt unimportant uint16_t
 * words in mid-sector so that the alpha and sparc checksums match,
 * and so the Sun magic number is embedded in the alpha checksum.
 *
 * The last uint64_t in the sector is the alpha arithmetic checksum.
 * The last uint16_t in the sector is the sun xor checksum.
 * The penultimate uint16_t in the sector is the sun magic number.
 *
 *	A:   511     510     509     508     507     506     505     504
 *	S:   510     511     508     509     506     507     504     505
 *	63     :       :       :     32:31     :       :       :       0
 *	|      :       :       :      \:|      :       :       :       |
 *	7654321076543210765432107654321076543210765432107654321076543210
 *	|--  sparc   --||--  sparc   --|
 *	|-- checksum --||--  magic   --|
 *	|----------------------- alpha checksum -----------------------|
 *			1011111011011010
 *			   b   e   d   a
 */

static void
resum(ib_params *params, struct alpha_boot_block * const bb, uint16_t *bb16)
{
	static uint64_t lastsum;

	if (bb16 != NULL)
		memcpy(bb, bb16, sizeof(*bb));
	ALPHA_BOOT_BLOCK_CKSUM(bb, &bb->bb_cksum);
	if (bb16 != NULL)
		memcpy(bb16, bb, sizeof(*bb));
	if ((params->flags & IB_VERBOSE) && lastsum != bb->bb_cksum)
		printf("alpha checksum now %016llx\n",
		    (unsigned long long)le64toh(bb->bb_cksum));
	lastsum = bb->bb_cksum;
}

static void
sun_bootstrap(ib_params *params, struct alpha_boot_block * const bb)
{
#	define BB_ADJUST_OFFSET 64
	static char our_int16s[] = "\2\3\6\7\12";
	uint16_t i, j, chkdelta, sunsum, bb16[256];

	/*
	 * Theory: the alpha checksum is adjusted so bits 47:32 add up
	 * to the Sun magic number. Then, another adjustment is computed
	 * so bits 63:48 add up to the Sun checksum, and applied in pieces
	 * so it changes the alpha checksum but not the Sun value.
	 *
  	 * Note: using memcpy(3) instead of a union as a strict c89/c9x
  	 * conformance experiment and to avoid a public interface delta.
	 */
	assert(sizeof(bb16) == sizeof(*bb));
	memcpy(bb16, bb, sizeof(bb16));
	for (i = 0; our_int16s[i]; ++i) {
		j = BB_ADJUST_OFFSET + our_int16s[i];
		if (bb16[j]) {
			warnx("Non-zero bits %04x in bytes %d..%d",
			    bb16[j], j * 2, j * 2 + 1);
			bb16[j] = 0;
			resum(params, bb, bb16);
		}
	}
	/*
	 * Make alpha checksum <47:32> come out to the sun magic.
	 */
	bb16[BB_ADJUST_OFFSET + 2] = htobe16(SUN_DKMAGIC) - bb16[254];
	resum(params, bb, bb16);
	sunsum = compute_sunsum(bb16);		/* might be the final value */
	if (params->flags & IB_VERBOSE)
		printf("target sun checksum is %04x\n", sunsum);
	/*
	 * Arrange to have alpha 63:48 add up to the sparc checksum.
	 */
	chkdelta = sunsum - bb16[255];
	bb16[BB_ADJUST_OFFSET + 3] = chkdelta >> 1;
	bb16[BB_ADJUST_OFFSET + 7] = chkdelta >> 1;
	/*
	 * By placing half the correction in two different uint64_t words at
	 * positions 63:48, the sparc sum will not change but the alpha sum
	 * will have the full correction, but only if the target adjustment
	 * was even. If it was odd, reverse propagate the carry one place.
	 */
	if (chkdelta & 1) {
		if (params->flags & IB_VERBOSE)
			printf("target adjustment %04x was odd, correcting\n",
			    chkdelta);
		assert(bb16[BB_ADJUST_OFFSET + 6] == 0);
		assert(bb16[BB_ADJUST_OFFSET + 012] == 0);
		bb16[BB_ADJUST_OFFSET + 6] += 0x8000;
		bb16[BB_ADJUST_OFFSET + 012] += 0x8000;
	}
	resum(params, bb, bb16);
	if (params->flags & IB_VERBOSE)
		printf("final harmonized checksum: %016llx\n",
		    (unsigned long long)le64toh(bb->bb_cksum));
	check_sparc(bb, "Final");
}

static void
check_sparc(const struct alpha_boot_block * const bb, const char *when)
{
	uint16_t bb16[256];
#define wmsg "%s sparc %s 0x%04x invalid, expected 0x%04x"

	memcpy(bb16, bb, sizeof(bb16));
	if (compute_sunsum(bb16) != bb16[255])
		warnx(wmsg, when, "checksum", bb16[255], compute_sunsum(bb16));
	if (bb16[254] != htobe16(SUN_DKMAGIC))
		warnx(wmsg, when, "magic number", bb16[254],
		    htobe16(SUN_DKMAGIC));
}