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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/*	$NetBSD: methods.c,v 1.8 2018/01/23 21:06:25 sevan Exp $	*/

/*-
 * Copyright (c) 1999 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Minoura Makoto.
 *
 * 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.
 */

#include <stdio.h>
#include <string.h>
#include <err.h>
#include <sys/types.h>

#include "memswitch.h"
#include "methods.h"

int
atoi_(const char **p)
{
	const char *p1 = *p;
	int v = 0;
	int first = 1;

	while (*p1 == ' ' || *p1 == '\t')
		p1++;

	if (*p1 == 0) {
		*p = 0;
		return 0;
	}
	if (strlen(p1) >= 2 && strncasecmp("0x", p1, 2) == 0) {
		p1 += 2;
		while (1) {
			if (*p1 >= '0' && *p1 <= '9') {
				v *= 16;
				v += *p1 - '0';
				first = 0;
			} else if (*p1 >= 'A' && *p1 <= 'F') {
				v *= 16;
				v += *p1 - 'A' + 10;
				first = 0;
			} else if (*p1 >= 'a' && *p1 <= 'f') {
				v *= 16;
				v += *p1 - 'a' + 10;
				first = 0;
			} else {
				break;
			}
			p1++;
		}
	} else {
		while (1) {
			if (*p1 >= '0' && *p1 <= '9') {
				v *= 10;
				v += *p1 - '0';
				first = 0;
			} else {
				break;
			}
			p1++;
		}
	}

	if (first) {
		*p = 0;
		return 0;
	}

	while (*p1 == ' ' || *p1 == '\t') p1++;
	*p = p1;
	return v;
}

int
fill_uchar(struct property *prop)
{
	if (current_values == 0)
		alloc_current_values();

	prop->current_value.byte[0] = current_values[prop->offset];
	prop->current_value.byte[1] = 0;
	prop->current_value.byte[2] = 0;
	prop->current_value.byte[3] = 0;
	prop->value_valid = 1;

	return 0;
}

int
fill_ushort(struct property *prop)
{
	if (current_values == 0)
		alloc_current_values();

	prop->current_value.byte[0] = current_values[prop->offset];
	prop->current_value.byte[1] = current_values[prop->offset+1];
	prop->current_value.byte[2] = 0;
	prop->current_value.byte[3] = 0;
	prop->value_valid = 1;

	return 0;
}

int
fill_ulong(struct property *prop)
{
	if (current_values == 0)
		alloc_current_values();

	prop->current_value.byte[0] = current_values[prop->offset];
	prop->current_value.byte[1] = current_values[prop->offset+1];
	prop->current_value.byte[2] = current_values[prop->offset+2];
	prop->current_value.byte[3] = current_values[prop->offset+3];
	prop->value_valid = 1;

	return 0;
}

int
flush_uchar(struct property *prop)
{
	if (!prop->modified)
		return 0;

	if (modified_values == 0)
		alloc_modified_values();

	modified_values[prop->offset] = prop->modified_value.byte[0];

	return 0;
}

int
flush_ushort(struct property *prop)
{
	if (!prop->modified)
		return 0;

	if (modified_values == 0)
		alloc_modified_values();

	modified_values[prop->offset] = prop->modified_value.byte[0];
	modified_values[prop->offset+1] = prop->modified_value.byte[1];

	return 0;
}

int
flush_ulong(struct property *prop)
{
	if (!prop->modified)
		return 0;

	if (modified_values == 0)
		alloc_modified_values();

	modified_values[prop->offset] = prop->modified_value.byte[0];
	modified_values[prop->offset+1] = prop->modified_value.byte[1];
	modified_values[prop->offset+2] = prop->modified_value.byte[2];
	modified_values[prop->offset+3] = prop->modified_value.byte[3];

	return 0;
}

int
flush_dummy(struct property *prop)
{
	return 0;
}

int
parse_dummy(struct property *prop, const char *value)
{
	warnx("Cannot modify %s.%s", prop->class, prop->node);

	return -1;
}

int
parse_byte(struct property *prop, const char *value)
{
	const char *p = value;
	int v;

	v = atoi_(&p);
	if (p == 0) {
		warnx("%s: Invalid value", value);
		return -1;
	}

	if (strcasecmp("MB", p) == 0)
		v *= 1024 * 1024;
	else if (strcasecmp("KB", p) == 0)
		v *= 1024;
	else if (*p != 0 &&
		 strcasecmp("B", p) != 0) {
		warnx("%s: Invalid value", value);
		return -1;
	}

	if (v < prop->min) {
		warnx("%s: Too small", value);
		return -1;
	} else if (v > prop->max) {
		warnx("%s: Too large", value);
		return -1;
	}

	prop->modified = 1;
	prop->modified_value.longword = v;

	return 0;
}

int
parse_uchar(struct property *prop, const char *value)
{
	const char *p = value;
	int v;

	v = atoi_(&p);
	if (p == 0) {
		warnx("%s: Invalid value", value);
		return -1;
	}

	if (v < prop->min) {
		warnx("%s: Too small", value);
		return -1;
	} else if (v > prop->max) {
		warnx("%s: Too large", value);
		return -1;
	}

	prop->modified = 1;
	prop->modified_value.byte[0] = v;

	return 0;
}

int
parse_ulong(struct property *prop, const char *value)
{
	const char *p = value;
	int v;

	v = atoi_(&p);
	if (p == 0) {
		warnx("%s: Invalid value", value);
		return -1;
	}

	if (v < prop->min) {
		warnx("%s: Too small", value);
		return -1;
	} else if (v > prop->max) {
		warnx("%s: Too large", value);
		return -1;
	}

	prop->modified = 1;
	prop->modified_value.longword = v;

	return 0;
}

int
parse_ushort(struct property *prop, const char *value)
{
	const char *p = value;
	int v;

	v = atoi_(&p);
	if (p == 0) {
		warnx("%s: Invalid value", value);
		return -1;
	}

	if (v < prop->min) {
		warnx("%s: Too small", value);
		return -1;
	} else if (v > prop->max) {
		warnx("%s: Too large", value);
		return -1;
	}

	prop->modified = 1;
	prop->modified_value.word[0] = v;

	return 0;
}

int
parse_time(struct property *prop, const char *value)
{
	const char *p = value;
	int v;

	while (*p == ' ' || *p == '\t') p++;
	if (*p == '-') {
		p++;
		v = -atoi_(&p);
	} else
		v = atoi_(&p);
	if (p == 0) {
		warnx("%s: Invalid value", value);
		return -1;
	}

	if (strcasecmp("hours", p) == 0 || strcasecmp("hour", p) == 0)
		v *= 60 * 60;
	else if (strcasecmp("minutes", p) == 0 ||
		 strcasecmp("minute", p) == 0)
		v *= 60;
	else if (*p != 0 &&
		 strcasecmp("second", p) != 0 &&
		 strcasecmp("seconds", p) != 0) {
		warnx("%s: Invalid value", value);
		return -1;
	}

	if (v < prop->min) {
		warnx("%s: Too small", value);
		return -1;
	} else if (v > prop->max) {
		warnx("%s: Too large", value);
		return -1;
	}

	prop->modified = 1;
	prop->modified_value.longword = v;

	return 0;
}

int
parse_bootdev(struct property *prop, const char *value)
{
	const char *p = value;
	int v;
	char expr_scsi[32];

	while (*p == ' ' || *p == '\t') p++;

	if (strcasecmp("STD", p) == 0)
		v = 0;
	else if (strcasecmp("ROM", p) == 0)
		v = 0xa000;
	else if (strcasecmp("RAM", p) == 0)
		v = 0xb000;
	else if (strncasecmp("HD", p, 2) == 0) {
		p += 2;
		v = atoi_(&p);
		if (p == 0 || v < 0 || v > 15) {
			warnx("%s: Invalid value", value);
			return -1;
		}
		v *= 0x0100;
		v += 0x8000;
	} else if (strncasecmp("FD", p, 2) == 0) {
		p += 2;
		v = atoi_(&p);
		if (p == 0 || v < 0 || v > 3) {
			warnx("%s: Invalid value", value);
			return -1;
		}
		v *= 0x0100;
		v += 0x9070;
	} else if (strncasecmp("INSCSI", p, 6) == 0 ||
		   strncasecmp("EXSCSI", p, 6) == 0) {
		int isin = strncasecmp("EXSCSI", p, 6);

		p += 6;
		v = atoi_(&p);
		if (p == 0 || v < 0 || v > 7) {
			warnx("%s: Invalid value", value);
			return -1;
		}

		/* change boot.romaddr */
		sprintf(expr_scsi, "boot.romaddr=0x%06x",
			(isin ? 0xfc0000 : 0xea0020) + v * 4);
		modify_single(expr_scsi);

		/* boot.device again */
		v = 0xa000;
	} else {
		warnx("%s: Invalid value", value);
		return -1;
	}

	prop->modified = 1;
	prop->modified_value.word[0] = v;

	return 0;
}

int
parse_serial(struct property *prop, const char *value)
#define NEXTSPEC	while (*p == ' ' || *p == '\t') p++;		\
			if (*p++ != ',') {				\
				warnx("%s: Invalid value", value);	\
				return -1;				\
			}						\
			while (*p == ' ' || *p == '\t') p++;
{
	const char *p = value;
	const char *q;
	int baud, bit, parity, stop, flow;
	static const int bauds[] = {75, 150, 300, 600, 1200, 2400, 4800, 9600,
	    17361, 0};
	static const char parities[] = "noe";
	int i;

	while (*p == ' ' || *p == '\t') p++;

	/* speed */
	baud = atoi_(&p);
	if (p == 0) {
		warnx("%s: Invalid value", value);
		return -1;
	}
	for (i = 0; bauds[i]; i++)
		if (baud == bauds[i])
			break;
	if (bauds[i] == 0) {
		warnx("%d: Invalid speed", baud);
		return -1;
	}
	baud = i;

	NEXTSPEC;

	/* bit size */
	if (*p < '5' || *p > '8') {
		warnx("%c: Invalid bit size", *p);
		return -1;
	}
	bit = *p++ - '5';

	NEXTSPEC;

	/* parity */
	q = strchr(parities, *p++);
	if (q == 0) {
		warnx("%c: Invalid parity spec", *p);
		return -1;
	}
	parity = q - parities;

	NEXTSPEC;

	/* stop bit */
	if (strncmp(p, "1.5", 3) == 0) {
		stop = 2;
		p += 3;
	} else if (strncmp(p, "2", 1) == 0) {
		stop = 0;
		p++;
	} else if (strncmp(p, "1", 1) == 0) {
		stop = 1;
		p++;
	} else {
		warnx("%s: Invalid value", value);
		return -1;
	}

	NEXTSPEC;

	/* flow */
	if (*p == '-')
		flow = 0;
	else if (*p == 's')
		flow = 1;
	else {
		warnx("%s: Invalid value", value);
		return -1;
	}

	p++;
	while (*p == ' ' || *p == '\t') p++;
	if (*p != 0) {
		warnx("%s: Invalid value", value);
		return -1;
	}

	prop->modified = 1;
	prop->modified_value.word[0] = ((stop << 14) +
					(parity << 12) +
					(bit << 10) +
					(flow << 9) +
					baud);

	return 0;
}
#undef NEXTSPEC

int
parse_srammode(struct property *prop, const char *value)
{
	static const char *const sramstrs[] = {"unused", "SRAMDISK", "program"};
	int i;

	for (i = 0; i <= 2; i++) {
		if (strcasecmp(value, sramstrs[i]) == 0)
			break;
	}
	if (i > 2) {
		warnx("%s: Invalid value", value);
		return -1;
	}

	prop->modified = 1;
	prop->modified_value.byte[0] = i;

	return 0;
}

int
print_uchar(struct property *prop, char *str)
{
	if (prop->modified)
		snprintf(str, MAXVALUELEN,
			 "%d", prop->modified_value.byte[0]);
	else {
		if (!prop->value_valid)
			prop->fill(prop);
		snprintf(str, MAXVALUELEN, "%d",
			 prop->current_value.byte[0]);
	}

	return 0;
}

int
print_ucharh(struct property *prop, char *str)
{
	if (prop->modified)
		snprintf(str, MAXVALUELEN,
			 "0x%4.4x", prop->modified_value.byte[0]);
	else {
		if (!prop->value_valid)
			prop->fill(prop);
		snprintf(str, MAXVALUELEN,
			 "0x%4.4x", prop->current_value.byte[0]);
	}

	return 0;
}

int
print_ushorth(struct property *prop, char *str)
{
	if (prop->modified)
		snprintf(str, MAXVALUELEN,
			  "0x%4.4x", prop->modified_value.word[0]);
	else {
		if (!prop->value_valid)
			prop->fill(prop);
		snprintf(str, MAXVALUELEN,
			 "0x%4.4x", prop->current_value.word[0]);
	}

	return 0;
}

int
print_ulong(struct property *prop, char *str)
{
	if (prop->modified)
		snprintf(str, MAXVALUELEN,
			 "%ld", prop->modified_value.longword);
	else {
		if (!prop->value_valid)
			prop->fill(prop);
		snprintf(str, MAXVALUELEN,
			 "%ld", prop->current_value.longword);
	}

	return 0;
}

int
print_ulongh(struct property *prop, char *str)
{
	if (prop->modified)
		snprintf(str, MAXVALUELEN,
			 "0x%8.8lx", prop->modified_value.longword);
	else {
		if (!prop->value_valid)
			prop->fill(prop);
		snprintf(str, MAXVALUELEN,
			 "0x%8.8lx", prop->current_value.longword);
	}

	return 0;
}

int
print_magic(struct property *prop, char *str)
{
	if (!prop->value_valid)
		prop->fill(prop);
	snprintf(str, MAXVALUELEN, "%c%c%c%c",
		 prop->current_value.byte[0],
		 prop->current_value.byte[1],
		 prop->current_value.byte[2],
		 prop->current_value.byte[3]);

	return 0;
}

int
print_timesec(struct property *prop, char *str)
{
	if (prop->modified)
		snprintf(str, MAXVALUELEN,
			 "%ld second", prop->modified_value.longword);
	else {
		if (!prop->value_valid)
			prop->fill(prop);
		snprintf(str, MAXVALUELEN,
			 "%ld second", prop->current_value.longword);
	}

	return 0;
}

int
print_bootdev(struct property *prop, char *str)
{
	unsigned int v;

	if (prop->modified)
		v = prop->modified_value.word[0];
	else {
		if (!prop->value_valid)
			prop->fill(prop);
		v = prop->current_value.word[0];
	}

	if (v == 0)
		strcpy(str, "STD");
	else if (v == 0xa000)
		strcpy(str, "ROM");
	else if (v == 0xb000)
		strcpy(str, "RAM");
	else if (v >= 0x8000 && v < 0x9000)
		snprintf(str, MAXVALUELEN, "HD%d", (v & 0x0f00) >> 8);
	else if (v >= 0x9000 && v < 0xa000)
		snprintf(str, MAXVALUELEN, "FD%d", (v & 0x0f00) >> 8);
	else
		snprintf(str, MAXVALUELEN, "%8.8x", v);

	return 0;
}

int
print_serial(struct property *prop, char *str)
{
	unsigned int v;
	const char *baud, *stop;
	char bit, parity, flow;
	static const char *const bauds[] = {"75", "150", "300", "600", "1200",
			       "2400", "4800", "9600", "17361"};
	static const char bits[] = "5678";
	static const char parities[] = "noen";
	static const char *const stops[] = {"2", "1", "1.5", "2"};
	static const char flows[] = "-s";

	if (prop->modified)
		v = prop->modified_value.word[0];
	else {
		if (!prop->value_valid)
			prop->fill(prop);
		v = prop->current_value.word[0];
	}

	baud = bauds[v & 0x000f];
	bit = bits[(v & 0x0c00) >> 10];
	parity = parities[(v & 0x3000) >> 12];
	stop = stops[(v & 0xe000) >> 14];
	flow = flows[(v & 0x0200) >> 9];
	sprintf(str, "%s,%c,%c,%s,%c", baud, bit, parity, stop, flow);

	return 0;
}

int
print_srammode(struct property *prop, char *str)
{
	int v;
	static const char *const sramstrs[] = {"unused", "SRAMDISK", "program"};

	if (prop->modified)
		v = prop->modified_value.byte[0];
	else {
		if (!prop->value_valid)
			prop->fill(prop);
		v = prop->current_value.byte[0];
	}

	if (v < 0 || v > 2)
		strcpy(str, "INVALID");
	else
		strcpy(str, sramstrs[v]);

	return 0;
}