fwtool.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. [ "$IGNORE_MINOR_COMPAT" = 1 ] && return 0
  62. v "The device is supported, but the config is incompatible to the new image ($devicecompat->$imagecompat). Please upgrade without keeping config (sysupgrade -n)."
  63. [ -n "$compatmessage" ] && v "$compatmessage"
  64. return 1
  65. fi
  66. return 0
  67. fi
  68. done
  69. v "Device $device not supported by this image"
  70. local devices="Supported devices:"
  71. for k in $dev_keys; do
  72. json_get_var dev "$k"
  73. devices="$devices $dev"
  74. done
  75. v "$devices"
  76. return 1
  77. }