fwtool.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. # Step 1. check if oem_name file exist and is not empty
  38. # If the above is true store the contents (b3000) in $oem value for later
  39. [ -s /tmp/sysinfo/oem_name ] && oem="$(cat /tmp/sysinfo/oem_name)"
  40. device="$(cat /tmp/sysinfo/board_name)"
  41. devicecompat="$(uci -q get system.@system[0].compat_version)"
  42. [ -n "$devicecompat" ] || devicecompat="1.0"
  43. json_get_var imagecompat compat_version
  44. json_get_var compatmessage compat_message
  45. [ -n "$imagecompat" ] || imagecompat="1.0"
  46. # select correct supported list based on compat_version
  47. # (using this ensures that compatibility check works for devices
  48. # not knowing about compat-version)
  49. local supported=supported_devices
  50. [ "$imagecompat" != "1.0" ] && supported=new_supported_devices
  51. json_select $supported || return 1
  52. json_get_keys dev_keys
  53. for k in $dev_keys; do
  54. json_get_var dev "$k"
  55. # Step 2.
  56. # lets start with the original case [ "$dev" = "$device" ]
  57. # if the evaluated firmware is vanila openwrt, this evals as true -ie
  58. # [ ("$dev" == "glinet.gl-b3000") == ("$device" == "glinet,gl-b3000") ]
  59. # however if the firmware is oem then $dev = b3000 and the above check fails resulting
  60. # in the erroneous warnings.
  61. # so we add the secondary check [ "$dev" = "$oem" ];
  62. # If in Step 1 the oem_file was found and valid, the $oem == "b3000" so
  63. # [ ("$dev" == "b3000) == ("$oem" == "b3000") ] so firmware is valid oem
  64. if [ "$dev" = "$device" ] || [ "$dev" = "$oem" ]; then
  65. # major compat version -> no sysupgrade
  66. if [ "${devicecompat%.*}" != "${imagecompat%.*}" ]; then
  67. v "The device is supported, but this image is incompatible for sysupgrade based on the image version ($devicecompat->$imagecompat)."
  68. [ -n "$compatmessage" ] && v "$compatmessage"
  69. return 1
  70. fi
  71. # minor compat version -> sysupgrade with -n required
  72. # Step 3.
  73. # here we must check if $dev == $oem to use this native compatability check
  74. # so we add the check for [ "$dev" = "$oem" ]
  75. if (([ "${devicecompat#.*}" != "${imagecompat#.*}" ] || [ "$dev" = "$oem" ])) && [ "$SAVE_CONFIG" = "1" ]; then
  76. # Step 4.
  77. # here we have to gaurd against the default case, oem may exsist and default will pass
  78. # the original check [ "${devicecompat#.*}" != "${imagecompat#.*}" ] so we must
  79. # explicitly check $dev == $oem, if it is we update(reuse) the $devicecompat and imagecompat
  80. # variable to reflect the case - ( Openwrt -> OEM )
  81. [ "$dev" = "$oem" ] && devicecompat="Openwrt " && imagecompat=" OEM"
  82. [ "$IGNORE_MINOR_COMPAT" = 1 ] && return 0
  83. v "The device is supported, but the config is incompatible to the new image ($devicecompat->$imagecompat). Please upgrade without keeping config (sysupgrade -n)."
  84. [ -n "$compatmessage" ] && v "$compatmessage"
  85. return 1
  86. fi
  87. return 0
  88. fi
  89. done
  90. v "Device $device not supported by this image"
  91. local devices="Supported devices:"
  92. for k in $dev_keys; do
  93. json_get_var dev "$k"
  94. devices="$devices $dev"
  95. done
  96. v "$devices"
  97. return 1
  98. }