a07796c9dc91d5654979f262fc4d51ec42297755
[openwrt/staging/luka.git] / package / base-files / files / usr / libexec / validate_firmware_image
1 #!/bin/sh
2
3 . /lib/functions.sh
4 . /lib/functions/system.sh
5 . /usr/share/libubox/jshn.sh
6
7 include /lib/upgrade
8
9 VALID=1
10 FORCEABLE=1
11
12 # Mark image as invalid but still possible to install
13 notify_firmware_invalid() {
14 VALID=0
15 }
16
17 # Mark image as broken (impossible to install)
18 notify_firmware_broken() {
19 VALID=0
20 FORCEABLE=0
21 }
22
23 # Add result of validation test
24 notify_firmware_test_result() {
25 local old_ns
26
27 json_set_namespace validate_firmware_image old_ns
28 json_add_boolean "$1" "$2"
29 json_set_namespace $old_ns
30 }
31
32 err_to_bool() {
33 [ "$1" -ne 0 ] && echo 0 || echo 1
34 }
35
36 fwtool_check_signature "$1" >&2
37 FWTOOL_SIGNATURE=$?
38 [ "$FWTOOL_SIGNATURE" -ne 0 ] && notify_firmware_invalid
39
40 fwtool_check_image "$1" >&2
41 FWTOOL_DEVICE_MATCH=$?
42 [ "$FWTOOL_DEVICE_MATCH" -ne 0 ] && notify_firmware_invalid
43
44 json_set_namespace validate_firmware_image old_ns
45 json_init
46 json_add_object "tests"
47 json_add_boolean fwtool_signature "$(err_to_bool $FWTOOL_SIGNATURE)"
48 json_add_boolean fwtool_device_match "$(err_to_bool $FWTOOL_DEVICE_MATCH)"
49
50 # Call platform_check_image() here so it can add its test
51 # results and still mark image properly.
52 json_set_namespace $old_ns
53 platform_check_image "$1" >&2 || notify_firmware_invalid
54 json_set_namespace validate_firmware_image old_ns
55 json_close_object
56 json_add_boolean valid "$VALID"
57 json_add_boolean forceable "$FORCEABLE"
58 json_dump -i
59 json_set_namespace $old_ns