uci-defaults.sh 16 KB

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