lantiq: do not set default firmware file
[openwrt/openwrt.git] / package / base-files / files / lib / functions / uci-defaults.sh
1 #!/bin/ash
2
3 CFG=/etc/board.json
4
5 . /lib/functions.sh
6 . /usr/share/libubox/jshn.sh
7
8 json_select_array() {
9 local _json_no_warning=1
10
11 json_select "$1"
12 [ $? = 0 ] && return
13
14 json_add_array "$1"
15 json_close_array
16
17 json_select "$1"
18 }
19
20 json_select_object() {
21 local _json_no_warning=1
22
23 json_select "$1"
24 [ $? = 0 ] && return
25
26 json_add_object "$1"
27 json_close_object
28
29 json_select "$1"
30 }
31
32 _ucidef_set_interface() {
33 local name="$1"
34 local iface="$2"
35 local proto="$3"
36
37 json_select_object "$name"
38 json_add_string ifname "$iface"
39
40 if ! json_is_a protocol string; then
41 case "$proto" in
42 static|dhcp|none|pppoe) : ;;
43 *)
44 case "$name" in
45 lan) proto="static" ;;
46 wan) proto="dhcp" ;;
47 *) proto="none" ;;
48 esac
49 ;;
50 esac
51
52 json_add_string protocol "$proto"
53 fi
54
55 json_select ..
56 }
57
58 ucidef_set_board_id() {
59 json_select_object model
60 json_add_string id "$1"
61 json_select ..
62 }
63
64 ucidef_set_model_name() {
65 json_select_object model
66 json_add_string name "$1"
67 json_select ..
68 }
69
70 ucidef_set_interface_lan() {
71 json_select_object network
72 _ucidef_set_interface lan "$@"
73 json_select ..
74 }
75
76 ucidef_set_interface_wan() {
77 json_select_object network
78 _ucidef_set_interface wan "$@"
79 json_select ..
80 }
81
82 ucidef_set_interfaces_lan_wan() {
83 local lan_if="$1"
84 local wan_if="$2"
85
86 json_select_object network
87 _ucidef_set_interface lan "$lan_if"
88 _ucidef_set_interface wan "$wan_if"
89 json_select ..
90 }
91
92 _ucidef_add_switch_port() {
93 # inherited: $num $device $need_tag $role $index $prev_role
94 # inherited: $n_cpu $n_ports $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5
95
96 n_ports=$((n_ports + 1))
97
98 json_select_array ports
99 json_add_object
100 json_add_int num "$num"
101 [ -n "$device" ] && json_add_string device "$device"
102 [ -n "$need_tag" ] && json_add_boolean need_tag "$need_tag"
103 [ -n "$role" ] && json_add_string role "$role"
104 [ -n "$index" ] && json_add_int index "$index"
105 json_close_object
106 json_select ..
107
108 # record pointer to cpu entry for lookup in _ucidef_finish_switch_roles()
109 [ -n "$device" ] && {
110 export "cpu$n_cpu=$n_ports"
111 n_cpu=$((n_cpu + 1))
112 }
113
114 # create/append object to role list
115 [ -n "$role" ] && {
116 json_select_array roles
117
118 if [ "$role" != "$prev_role" ]; then
119 json_add_object
120 json_add_string role "$role"
121 json_add_string ports "$num"
122 json_close_object
123
124 prev_role="$role"
125 n_vlan=$((n_vlan + 1))
126 else
127 json_select_object "$n_vlan"
128 json_get_var port ports
129 json_add_string ports "$port $num"
130 json_select ..
131 fi
132
133 json_select ..
134 }
135 }
136
137 _ucidef_finish_switch_roles() {
138 # inherited: $name $n_cpu $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5
139 local index role roles num device need_tag port ports
140
141 json_select switch
142 json_select "$name"
143 json_get_keys roles roles
144 json_select ..
145 json_select ..
146
147 for index in $roles; do
148 eval "port=\$cpu$(((index - 1) % n_cpu))"
149
150 json_select switch
151 json_select "$name"
152 json_select ports
153 json_select "$port"
154 json_get_vars num device need_tag
155 json_select ..
156 json_select ..
157
158 if [ $n_vlan -gt $n_cpu -o ${need_tag:-0} -eq 1 ]; then
159 num="${num}t"
160 device="${device}.${index}"
161 fi
162
163 json_select roles
164 json_select "$index"
165 json_get_vars role ports
166 json_add_string ports "$ports $num"
167 json_add_string device "$device"
168 json_select ..
169 json_select ..
170 json_select ..
171 json_select ..
172
173 json_select_object network
174 local devices
175
176 json_select_object "$role"
177 # attach previous interfaces (for multi-switch devices)
178 json_get_var devices ifname
179 if ! list_contains devices "$device"; then
180 devices="${devices:+$devices }$device"
181 fi
182 json_select ..
183
184 _ucidef_set_interface "$role" "$devices"
185 json_select ..
186 done
187 }
188
189 ucidef_add_switch() {
190 local name="$1"; shift
191 local port num role device index need_tag prev_role
192 local cpu0 cpu1 cpu2 cpu3 cpu4 cpu5
193 local n_cpu=0 n_vlan=0 n_ports=0
194
195 json_select_object switch
196 json_select_object "$name"
197 json_add_boolean enable 1
198 json_add_boolean reset 1
199
200 for port in "$@"; do
201 case "$port" in
202 [0-9]*@*)
203 num="${port%%@*}"
204 device="${port##*@}"
205 need_tag=0
206 [ "${num%t}" != "$num" ] && {
207 num="${num%t}"
208 need_tag=1
209 }
210 ;;
211 [0-9]*:*:[0-9]*)
212 num="${port%%:*}"
213 index="${port##*:}"
214 role="${port#[0-9]*:}"; role="${role%:*}"
215 ;;
216 [0-9]*:*)
217 num="${port%%:*}"
218 role="${port##*:}"
219 ;;
220 esac
221
222 if [ -n "$num" ] && [ -n "$device$role" ]; then
223 _ucidef_add_switch_port
224 fi
225
226 unset num device role index need_tag
227 done
228 json_select ..
229 json_select ..
230
231 _ucidef_finish_switch_roles
232 }
233
234 ucidef_add_switch_attr() {
235 local name="$1"
236 local key="$2"
237 local val="$3"
238
239 json_select_object switch
240 json_select_object "$name"
241
242 case "$val" in
243 true|false) [ "$val" != "true" ]; json_add_boolean "$key" $? ;;
244 [0-9]) json_add_int "$key" "$val" ;;
245 *) json_add_string "$key" "$val" ;;
246 esac
247
248 json_select ..
249 json_select ..
250 }
251
252 ucidef_add_switch_port_attr() {
253 local name="$1"
254 local port="$2"
255 local key="$3"
256 local val="$4"
257 local ports i num
258
259 json_select_object switch
260 json_select_object "$name"
261
262 json_get_keys ports ports
263 json_select_array ports
264
265 for i in $ports; do
266 json_select "$i"
267 json_get_var num num
268
269 if [ -n "$num" ] && [ $num -eq $port ]; then
270 json_select_object attr
271
272 case "$val" in
273 true|false) [ "$val" != "true" ]; json_add_boolean "$key" $? ;;
274 [0-9]) json_add_int "$key" "$val" ;;
275 *) json_add_string "$key" "$val" ;;
276 esac
277
278 json_select ..
279 fi
280
281 json_select ..
282 done
283
284 json_select ..
285 json_select ..
286 json_select ..
287 }
288
289 ucidef_set_interface_macaddr() {
290 local network="$1"
291 local macaddr="$2"
292
293 json_select_object network
294
295 json_select "$network"
296 [ $? -eq 0 ] || {
297 json_select ..
298 return
299 }
300
301 json_add_string macaddr "$macaddr"
302 json_select ..
303
304 json_select ..
305 }
306
307 ucidef_add_atm_bridge() {
308 local vpi="$1"
309 local vci="$2"
310 local encaps="$3"
311 local payload="$4"
312
313 json_select_object dsl
314 json_select_object atmbridge
315 json_add_int vpi "$vpi"
316 json_add_int vci "$vci"
317 json_add_string encaps "$encaps"
318 json_add_string payload "$payload"
319 json_select ..
320 json_select ..
321 }
322
323 ucidef_add_adsl_modem() {
324 local annex="$1"
325 local firmware="$2"
326
327 json_select_object dsl
328 json_select_object modem
329 json_add_string type "adsl"
330 json_add_string annex "$annex"
331 json_add_string firmware "$firmware"
332 json_select ..
333 json_select ..
334 }
335
336 ucidef_add_vdsl_modem() {
337 local annex="$1"
338 local tone="$2"
339 local xfer_mode="$3"
340
341 json_select_object dsl
342 json_select_object modem
343 json_add_string type "vdsl"
344 json_add_string annex "$annex"
345 json_add_string tone "$tone"
346 json_add_string xfer_mode "$xfer_mode"
347 json_select ..
348 json_select ..
349 }
350
351 ucidef_set_led_netdev() {
352 local cfg="led_$1"
353 local name="$2"
354 local sysfs="$3"
355 local dev="$4"
356 local mode="${5:-link tx rx}"
357
358 json_select_object led
359
360 json_select_object "$1"
361 json_add_string name "$name"
362 json_add_string type netdev
363 json_add_string sysfs "$sysfs"
364 json_add_string device "$dev"
365 json_add_string mode "$mode"
366 json_select ..
367
368 json_select ..
369 }
370
371 ucidef_set_led_usbdev() {
372 local cfg="led_$1"
373 local name="$2"
374 local sysfs="$3"
375 local dev="$4"
376
377 json_select_object led
378
379 json_select_object "$1"
380 json_add_string name "$name"
381 json_add_string type usb
382 json_add_string sysfs "$sysfs"
383 json_add_string device "$dev"
384 json_select ..
385
386 json_select ..
387 }
388
389 ucidef_set_led_wlan() {
390 local cfg="led_$1"
391 local name="$2"
392 local sysfs="$3"
393 local trigger="$4"
394
395 json_select_object led
396
397 json_select_object "$1"
398 json_add_string name "$name"
399 json_add_string type trigger
400 json_add_string sysfs "$sysfs"
401 json_add_string trigger "$trigger"
402 json_select ..
403
404 json_select ..
405 }
406
407 ucidef_set_led_switch() {
408 local cfg="led_$1"
409 local name="$2"
410 local sysfs="$3"
411 local trigger="$4"
412 local port_mask="$5"
413 local speed_mask="$6"
414
415 json_select_object led
416
417 json_select_object "$1"
418 json_add_string name "$name"
419 json_add_string type switch
420 json_add_string sysfs "$sysfs"
421 json_add_string trigger "$trigger"
422 json_add_string port_mask "$port_mask"
423 json_add_string speed_mask "$speed_mask"
424 json_select ..
425
426 json_select ..
427 }
428
429 ucidef_set_led_default() {
430 local cfg="led_$1"
431 local name="$2"
432 local sysfs="$3"
433 local default="$4"
434
435 json_select_object led
436
437 json_select_object "$1"
438 json_add_string name "$name"
439 json_add_string sysfs "$sysfs"
440 json_add_string default "$default"
441 json_select ..
442
443 json_select ..
444 }
445
446 ucidef_set_led_gpio() {
447 local cfg="led_$1"
448 local name="$2"
449 local sysfs="$3"
450 local gpio="$4"
451 local inverted="$5"
452
453 json_select_object led
454
455 json_select_object "$1"
456 json_add_string type gpio
457 json_add_string name "$name"
458 json_add_string sysfs "$sysfs"
459 json_add_string trigger "$trigger"
460 json_add_int gpio "$gpio"
461 json_add_boolean inverted "$inverted"
462 json_select ..
463
464 json_select ..
465 }
466
467 ucidef_set_led_ide() {
468 local cfg="led_$1"
469 local name="$2"
470 local sysfs="$3"
471
472 json_select_object led
473
474 json_select_object "$1"
475 json_add_string name "$name"
476 json_add_string sysfs "$sysfs"
477 json_add_string trigger ide-disk
478 json_select ..
479
480 json_select ..
481 }
482
483 __ucidef_set_led_timer() {
484 local cfg="led_$1"
485 local name="$2"
486 local sysfs="$3"
487 local trigger="$4"
488 local delayon="$5"
489 local delayoff="$6"
490
491 json_select_object led
492
493 json_select_object "$1"
494 json_add_string type "$trigger"
495 json_add_string name "$name"
496 json_add_string sysfs "$sysfs"
497 json_add_int delayon "$delayon"
498 json_add_int delayoff "$delayoff"
499 json_select ..
500
501 json_select ..
502 }
503
504 ucidef_set_led_oneshot() {
505 __ucidef_set_led_timer $1 $2 $3 "oneshot" $4 $5
506 }
507
508 ucidef_set_led_timer() {
509 __ucidef_set_led_timer $1 $2 $3 "timer" $4 $5
510 }
511
512 ucidef_set_led_rssi() {
513 local cfg="led_$1"
514 local name="$2"
515 local sysfs="$3"
516 local iface="$4"
517 local minq="$5"
518 local maxq="$6"
519 local offset="$7"
520 local factor="$8"
521
522 json_select_object led
523
524 json_select_object "$1"
525 json_add_string type rssi
526 json_add_string name "$name"
527 json_add_string iface "$iface"
528 json_add_string sysfs "$sysfs"
529 json_add_string minq "$minq"
530 json_add_string maxq "$maxq"
531 json_add_string offset "$offset"
532 json_add_string factor "$factor"
533 json_select ..
534
535 json_select ..
536 }
537
538 ucidef_set_rssimon() {
539 local dev="$1"
540 local refresh="$2"
541 local threshold="$3"
542
543 json_select_object rssimon
544
545 json_select_object "$dev"
546 [ -n "$refresh" ] && json_add_int refresh "$refresh"
547 [ -n "$threshold" ] && json_add_int threshold "$threshold"
548 json_select ..
549
550 json_select ..
551
552 }
553
554 ucidef_add_gpio_switch() {
555 local cfg="$1"
556 local name="$2"
557 local pin="$3"
558 local default="${4:-0}"
559
560 json_select_object gpioswitch
561 json_select_object "$cfg"
562 json_add_string name "$name"
563 json_add_int pin "$pin"
564 json_add_int default "$default"
565 json_select ..
566 json_select ..
567 }
568
569 board_config_update() {
570 json_init
571 [ -f ${CFG} ] && json_load "$(cat ${CFG})"
572
573 # auto-initialize model id and name if applicable
574 if ! json_is_a model object; then
575 json_select_object model
576 [ -f "/tmp/sysinfo/board_name" ] && \
577 json_add_string id "$(cat /tmp/sysinfo/board_name)"
578 [ -f "/tmp/sysinfo/model" ] && \
579 json_add_string name "$(cat /tmp/sysinfo/model)"
580 json_select ..
581 fi
582 }
583
584 board_config_flush() {
585 json_dump -i > /tmp/.board.json
586 mv /tmp/.board.json ${CFG}
587 }