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
|
implement Configflash;
#
# this isn't a proper config program: it's currently just
# enough to set important parameters such as ethernet address.
# an extension is in the works.
# --chf
include "sys.m";
sys: Sys;
include "draw.m";
include "string.m";
str: String;
Configflash: module
{
init: fn(nil: ref Draw->Context, args: list of string);
};
Region: adt {
base: int;
limit: int;
};
#
# structure of allocation descriptor
#
Fcheck: con 0;
Fbase: con 4;
Flen: con 8;
Ftag: con 11;
Fsig: con 12;
Fasize: con 3*4+3+1;
Tdead: con byte 0;
Tboot: con byte 16r01;
Tconf: con byte 16r02;
Tnone: con byte 16rFF;
flashsig := array[] of {byte 16rF1, byte 16rA5, byte 16r5A, byte 16r1F};
noval := array[] of {0 to 3 =>byte 16rFF}; #
Ctag, Cscreen, Cconsole, Cbaud, Cether, Cea, Cend: con iota;
config := array[] of {
Ctag => "#plan9.ini\n", # current flag for qboot, don't change
Cscreen => "vgasize=640x480x8\n",
Cconsole => "console=0 lcd\n",
Cbaud => "baud=9600\n",
Cether => "ether0=type=SCC port=2 ", # note missing \n
Cea => "ea=08003e400080\n",
Cend => "\0" # qboot currently requires it but shouldn't
};
Param: adt {
name: string;
index: int;
};
params := array[] of {
Param("vgasize", Cscreen),
Param("console", Cconsole),
Param("ea", Cea),
Param("baud", Cbaud)
};
# could come from file or #F/flash/flashctl
FLASHSEG: con 256*1024;
bootregion := Region(0, FLASHSEG);
stderr: ref Sys->FD;
prog := "qconfig";
damaged := 0;
debug := 0;
usage()
{
sys->fprint(stderr, "Usage: %s [-D] [-f flash] [-param value ...]\n", prog);
exit;
}
err(s: string)
{
sys->fprint(stderr, "%s: %s", prog, s);
if(!damaged)
sys->fprint(stderr, "; flash not modified\n");
else
sys->fprint(stderr, "; flash might now be invalid\n");
exit;
}
init(nil: ref Draw->Context, args: list of string)
{
sys = load Sys Sys->PATH;
sys->pctl(Sys->FORKFD|Sys->NEWPGRP, nil);
stderr = sys->fildes(2);
if(args != nil){
prog = hd args;
args = tl args;
}
str = load String String->PATH;
if(str == nil)
err(sys->sprint("can't load %s: %r", String->PATH));
flash := "#F/flash/flash";
offset := 0;
region := bootregion;
for(; args != nil && (hd args)[0] == '-'; args = tl args)
case a := hd args {
"-f" =>
(flash, args) = argf(tl args);
"-D" =>
debug = 1;
* =>
p := lookparam(params, a[1:]);
if(p.index < 0)
err(sys->sprint("unknown config parameter: %s", a));
v: string;
(v, args) = argf(tl args);
config[p.index] = a[1:]+"="+v+"\n"; # would be nice to check it
}
if(len args > 0)
usage();
out := sys->open(flash, Sys->ORDWR);
if(out == nil)
err(sys->sprint("can't open %s for read/write: %r", flash));
# TO DO: hunt for free space and add new entry
plonk(out, FLASHSEG-Fasize, mkdesc(0, 128*1024, Tboot));
c := flatten(config);
if(debug)
sys->print("%s", c);
bconf := array of byte c;
plonk(out, FLASHSEG-Fasize*2, mkdesc(128*1024, len bconf, Tconf));
plonk(out, 128*1024, bconf);
}
argf(args: list of string): (string, list of string)
{
if(args == nil)
usage();
return (hd args, args);
}
lookparam(options: array of Param, s: string): Param
{
for(i := 0; i < len options; i++)
if(options[i].name == s)
return options[i];
return Param(nil, -1);
}
flatten(a: array of string): string
{
s := "";
for(i := 0; i < len a; i++)
s += a[i];
return s;
}
plonk(out: ref Sys->FD, where: int, val: array of byte)
{
if(debug){
sys->print("write #%ux [%d]:", where, len val);
for(i:=0; i<len val; i++)
sys->print(" %.2ux", int val[i]);
sys->print("\n");
}
sys->seek(out, big where, 0);
if(sys->write(out, val, len val) != len val)
err(sys->sprint("bad flash write: %r"));
}
cvt(v: int): array of byte
{
a := array[4] of byte;
a[0] = byte (v>>24);
a[1] = byte (v>>16);
a[2] = byte (v>>8);
a[3] = byte (v & 16rff);
return a;
}
mkdesc(base: int, length: int, tag: byte): array of byte
{
a := array[Fasize] of byte;
a[Fcheck:] = noval;
a[Fbase:] = cvt(base);
a[Flen:] = cvt(length)[1:]; # it's three bytes
a[Ftag] = tag;
a[Fsig:] = flashsig;
return a;
}
|