base-files: validate firmware for compatibility with backup
[openwrt/openwrt.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 ALLOW_BACKUP=1
12
13 # Mark image as invalid but still possible to install
14 notify_firmware_invalid() {
15 VALID=0
16 }
17
18 # Mark image as broken (impossible to install)
19 notify_firmware_broken() {
20 VALID=0
21 FORCEABLE=0
22 }
23
24 # Mark image as incompatible with preserving a backup
25 notify_firmware_no_backup() {
26 ALLOW_BACKUP=0
27 }
28
29 # Add result of validation test
30 notify_firmware_test_result() {
31 local old_ns
32
33 json_set_namespace validate_firmware_image old_ns
34 json_add_boolean "$1" "$2"
35 json_set_namespace $old_ns
36 }
37
38 err_to_bool() {
39 [ "$1" -ne 0 ] && echo 0 || echo 1
40 }
41
42 fwtool_check_signature "$1" >&2
43 FWTOOL_SIGNATURE=$?
44 [ "$FWTOOL_SIGNATURE" -ne 0 ] && notify_firmware_invalid
45
46 fwtool_check_image "$1" >&2
47 FWTOOL_DEVICE_MATCH=$?
48 [ "$FWTOOL_DEVICE_MATCH" -ne 0 ] && notify_firmware_invalid
49
50 json_set_namespace validate_firmware_image old_ns
51 json_init
52 json_add_object "tests"
53 json_add_boolean fwtool_signature "$(err_to_bool $FWTOOL_SIGNATURE)"
54 json_add_boolean fwtool_device_match "$(err_to_bool $FWTOOL_DEVICE_MATCH)"
55
56 # Call platform_check_image() here so it can add its test
57 # results and still mark image properly.
58 json_set_namespace $old_ns
59 platform_check_image "$1" >&2 || notify_firmware_invalid
60 json_set_namespace validate_firmware_image old_ns
61 json_close_object
62 json_add_boolean valid "$VALID"
63 json_add_boolean forceable "$FORCEABLE"
64 json_add_boolean allow_backup "$ALLOW_BACKUP"
65 json_dump -i
66 json_set_namespace $old_ns