resflash.sub 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/bin/sh
  2. # Subroutines for all resflash tools
  3. # Copyright Brian Conway <bconway@rcesoftware.com>, see LICENSE for details
  4. # Check whether -e is superfluous for handling escape characters
  5. if [ $(echo "\n"|wc -l) -eq 2 ]; then
  6. ESCECHO=echo
  7. else
  8. ESCECHO='echo -e'
  9. fi
  10. # Assigns: NEXTVND
  11. get_next_vnd() {
  12. NEXTVND=$(vnconfig -l|grep 'not in use'|head -n 1|cut -d : -f 1)
  13. }
  14. umount_all() {
  15. sync
  16. for cleanupmnt in $(mount|tail -r|grep resflash|cut -d ' ' -f 3); do
  17. umount ${cleanupmnt}
  18. done
  19. for cleanupvnd in $(vnconfig -l|grep resflash|cut -d : -f 1); do
  20. vnconfig -u ${cleanupvnd}
  21. done
  22. }
  23. # Args: 1 - img or fs file
  24. # Assigns: MNTPATH
  25. mount_img_or_fs() {
  26. MNTPATH=$(mktemp -t -d resflash.XXXXXX)
  27. trap "umount_all; echo \*\*\* Error encountered. MNTPATH: ${MNTPATH} \*\*\*; \
  28. exit 1" ERR INT
  29. mkdir -p ${MNTPATH}/fs
  30. get_next_vnd
  31. mntvnd=${NEXTVND}
  32. vnconfig ${mntvnd} ${1} > /dev/null
  33. case $(echo ${1}|awk -F . -safe '{ print $NF }') in
  34. img) mkdir -p ${MNTPATH}/mbr ${MNTPATH}/cfg
  35. mount -o noatime /dev/${mntvnd}a ${MNTPATH}/mbr
  36. mount -o noatime /dev/${mntvnd}d ${MNTPATH}/fs
  37. mount -o noatime /dev/${mntvnd}f ${MNTPATH}/cfg
  38. if disklabel ${mntvnd}|grep -q 'i:.*MSDOS'; then
  39. mkdir -p ${MNTPATH}/msdos
  40. mount -o noatime /dev/${mntvnd}i ${MNTPATH}/msdos
  41. fi;;
  42. fs) mount -o noatime /dev/${mntvnd}c ${MNTPATH}/fs;;
  43. *) echo '*** ERROR: Not a resflash img or fs file. ***'
  44. vnconfig -u ${mntvnd}
  45. exit 1;;
  46. esac
  47. mount -t mfs -o noatime,nodev,noexec,-s256M swap ${MNTPATH}/fs/tmp
  48. }
  49. # Args: 1 - MACHINE
  50. # Assigns: ALG, DOS{PARTID,MNT,BOOTDIR,BOOTBINS,PARTMB}, FLAGPART, MBRPARTMB,
  51. # XXHALG
  52. set_attr_by_machine() {
  53. case ${1} in
  54. amd64) ALG=sha512/256
  55. DOSPARTID=EF
  56. DOSSTART=64
  57. DOSPARTMB=3
  58. FLAGPART=3
  59. DOSMNT=efi
  60. DOSBOOTDIR=efi/boot
  61. DOSBOOTBINS='BOOTX64.EFI BOOTIA32.EFI'
  62. MBRSTART=4
  63. MBRPARTMB=4;;
  64. i386) ALG=md5
  65. DOSPARTID=EF
  66. DOSSTART=64
  67. DOSPARTMB=3
  68. FLAGPART=3
  69. DOSMNT=efi
  70. DOSBOOTDIR=
  71. DOSBOOTBINS=
  72. MBRSTART=4
  73. MBRPARTMB=4;;
  74. arm64) ALG=sha512/256
  75. DOSPARTID=0C
  76. # This offset is required by Rockchip
  77. DOSSTART=$((16 * 1024 * 1024 / BYTESECT))
  78. DOSPARTMB=16
  79. FLAGPART=0
  80. DOSMNT=uboot
  81. DOSBOOTDIR=efi/boot
  82. DOSBOOTBINS='BOOTAA64.EFI'
  83. MBRSTART=32
  84. MBRPARTMB=4;;
  85. *) echo 'Unsupported arch.'
  86. exit 1;;
  87. esac
  88. if which xxhsum > /dev/null 2>&1; then
  89. XXHALG=3
  90. fi
  91. }