fwtool.sh 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. fwtool_check_signature() {
  2. [ $# -gt 1 ] && return 1
  3. [ ! -x /usr/bin/ucert ] && {
  4. if [ "$REQUIRE_IMAGE_SIGNATURE" = 1 ]; then
  5. return 1
  6. else
  7. return 0
  8. fi
  9. }
  10. if ! fwtool -q -s /tmp/sysupgrade.ucert "$1"; then
  11. v "Image signature not present"
  12. [ "$REQUIRE_IMAGE_SIGNATURE" = 1 -a "$FORCE" != 1 ] && {
  13. v "Use sysupgrade -F to override this check when downgrading or flashing to vendor firmware"
  14. }
  15. [ "$REQUIRE_IMAGE_SIGNATURE" = 1 ] && return 1
  16. return 0
  17. fi
  18. fwtool -q -T -s /dev/null "$1" | \
  19. ucert -V -m - -c "/tmp/sysupgrade.ucert" -P /etc/opkg/keys
  20. return $?
  21. }
  22. fwtool_check_image() {
  23. [ $# -gt 1 ] && return 1
  24. . /usr/share/libubox/jshn.sh
  25. if ! fwtool -q -i /tmp/sysupgrade.meta "$1"; then
  26. v "Image metadata not present"
  27. [ "$REQUIRE_IMAGE_METADATA" = 1 -a "$FORCE" != 1 ] && {
  28. v "Use sysupgrade -F to override this check when downgrading or flashing to vendor firmware"
  29. }
  30. [ "$REQUIRE_IMAGE_METADATA" = 1 ] && return 1
  31. return 0
  32. fi
  33. json_load "$(cat /tmp/sysupgrade.meta)" || {
  34. v "Invalid image metadata"
  35. return 1
  36. }
  37. device="$(cat /tmp/sysinfo/board_name)"
  38. devicecompat="$(uci -q get system.@system[0].compat_version)"
  39. [ -n "$devicecompat" ] || devicecompat="1.0"
  40. json_get_var imagecompat compat_version
  41. json_get_var compatmessage compat_message
  42. [ -n "$imagecompat" ] || imagecompat="1.0"
  43. # select correct supported list based on compat_version
  44. # (using this ensures that compatibility check works for devices
  45. # not knowing about compat-version)
  46. local supported=supported_devices
  47. [ "$imagecompat" != "1.0" ] && supported=new_supported_devices
  48. json_select $supported || return 1
  49. json_get_keys dev_keys
  50. for k in $dev_keys; do
  51. json_get_var dev "$k"
  52. if [ "$dev" = "$device" ]; then
  53. # major compat version -> no sysupgrade
  54. if [ "${devicecompat%.*}" != "${imagecompat%.*}" ]; then
  55. v "The device is supported, but this image is incompatible for sysupgrade based on the image version ($devicecompat->$imagecompat)."
  56. [ -n "$compatmessage" ] && v "$compatmessage"
  57. return 1
  58. fi
  59. # minor compat version -> sysupgrade with -n required
  60. if [ "${devicecompat#.*}" != "${imagecompat#.*}" ] && [ "$SAVE_CONFIG" = "1" ]; then
  61. v "The device is supported, but the config is incompatible to the new image ($devicecompat->$imagecompat). Please upgrade without keeping config (sysupgrade -n)."
  62. [ -n "$compatmessage" ] && v "$compatmessage"
  63. return 1
  64. fi
  65. return 0
  66. fi
  67. done
  68. v "Device $device not supported by this image"
  69. local devices="Supported devices:"
  70. for k in $dev_keys; do
  71. json_get_var dev "$k"
  72. devices="$devices $dev"
  73. done
  74. v "$devices"
  75. return 1
  76. }