script: add various helper

This commit is contained in:
Mathieu Maret 2019-05-17 12:49:42 +02:00
parent c7c2c2bbba
commit f2e173f932
10 changed files with 510 additions and 4 deletions

8
scripts/br_push.sh Executable file
View File

@ -0,0 +1,8 @@
#!/bin/bash
REMOTE=$(git remote)
BRANCH=$(git branch | grep '*' | sed 's/* //')
git push $REMOTE $BRANCH
exit 0

107
scripts/create_prima_disk.sh Executable file
View File

@ -0,0 +1,107 @@
#!/bin/bash
bootimg=""
rootimg=""
qemuroot=""
while getopts "b:r:q:" opt; do
case $opt in
b)
bootimg=$OPTARG
;;
r)
rootimg=$OPTARG
;;
q)
qemuroot=$OPTARG
;;
esac
done
create_empty_disk()
{
img_name=$1
echo "Creating disk image $img_name"
dd if=/dev/zero of=$img_name bs=4M count=300 > /dev/null
}
create_loopback()
{
img_name=$1
device=$(losetup -f)
sudo losetup $device $img_name
echo $device
}
get_partition_name()
{
echo "/dev/mapper/${1##*/}p"$2
}
format_sdcard()
{
device=$1
img_name=$2
system_size="+1100M"
boot_size="+70M"
set +e
mounts=`cat /proc/mounts | grep "$device" | cut -d' ' -f1`
set -e
if [ "$mounts" != "" ];then
for i in $mounts;
do
sudo umount $i -l
done
fi
echo "making partitions on ${device}..."
set +e
sleep 1
sudo fdisk $device > /tmp/log << EOF
n
p
1
$boot_size
n
p
2
$system_size
t
1
c
a
1
w
EOF
set -e
#With loop block devices, new partition are not automatically associated to a new loop partition. kpartx do that for us !
if [[ $device == *"loop"* ]]; then
sudo losetup -d $device
sudo kpartx -a $img_name
fi
echo "$(get_partition_name $device 1) formatting..."
part=$(get_partition_name $device 1)
sudo dd if=$bootimg of=$part > /dev/null
echo "$(get_partition_name $device 2) formatting... (takes some minutes)"
part=$(get_partition_name $device 2)
sudo dd if=$rootimg of=$part > /dev/null
tmp_dir=$(mktemp -d)
sudo mount $part $tmp_dir
sudo cp -r $qemuroot/target/lib/modules/4.9.6/ ${tmp_dir}/lib/modules
sudo umount $tmp_dir
rm -r $tmp_dir
}
name=disk.img
create_empty_disk $name
device=$(create_loopback $name)
format_sdcard ${device} ${name}
sudo kpartx -d ${name}

42
scripts/flash_uc Executable file
View File

@ -0,0 +1,42 @@
#!/bin/bash
set -euo pipefail
VERSION="1.0"
JLINK_INSTALL_PATH="/opt/SEGGER/JLink"
GDB_CMDS="target remote localhost:2331
monitor reset
monitor halt
load
compare-sections
q
"
jlinkPID=""
TMP=""
function finish {
if [ "$jlinkPID" != "" ]; then
echo "Stopping Jlink"
kill $jlinkPID
fi
if [ "$TMP" != "" ]; then
echo "Cleaning tmp files"
rm $TMP
fi
}
trap finish EXIT
echo "Version: $VERSION"
if [ ! -e "$1" ];then
>&2 echo "Cannot access $1, exiting"
exit 1
fi
"$JLINK_INSTALL_PATH"/JLinkGDBServer -select USB -device MKL02Z32xxx4 -endian little -if SWD -speed 1000 > /dev/null &
jlinkPID=$!
TMP=$(mktemp /tmp/XXXXX-gdbcmd)
echo "${GDB_CMDS}" > "$TMP"
gdb-multiarch -x "$TMP" "$1"

301
scripts/flash_whole_poc Executable file
View File

@ -0,0 +1,301 @@
#!/bin/bash
VERSION=1.0
THIS_COMPUTER_IP="192.168.0.230"
UBOOT_IMG_NAME="apf6-u-boot.img"
UBOOT_SPL_NAME="apf6-u-boot.spl"
TFTP_SERVER_PATH="/tftpboot"
BLOCK_SIZE=0x200
RED='\033[0;31m'
NC='\033[0m' # No Color
images=""
#images="apf6-boot.ext4;1 apf6-rootfs.ext4;2"
#images="boot.ext4;1 rootfs.ext2;2"
disk=""
tty=""
ubootimg=""
ums=0
set -euo pipefail
function waitForText
{
while read input
do
if [[ "$input" == *"$2"* ]]; then
echo "$input"
break;
fi
done < "$1"
}
function enterUboot
{
set +e
fuser "$1" > /dev/null 2>&1
if [ $? -eq 0 ]; then
>&2 echo "$1 is in use, exiting"
exit 1
fi
set -e
stty -F "$1" 115200 raw -echo -echoe -echok #-echoctl -echoke
while read input
do
if [[ "$input" == *"BIOS>" ]]; then
break;
fi
echo "" > "$1"
done < "$1"
}
function waitboot
{
waitForText "$1" "U-Boot SPL" > /dev/null
}
function flash_uboot
{
if [ ! -e "$2" ]; then
>&2 echo "Cannot access $2 img, exiting"
exit 1
fi
spl_img=${2%.*}.spl
if [ ! -e "$spl_img" ]; then
>&2 echo "Cannot access $spl_img img, exiting"
exit 1
fi
flashDiskNet "$1" "$spl_img;4 $2;3"
echo "Done"
echo "Please reboot the board"
waitboot "$1"
enterUboot "$1"
echo "Setting u-boot env"
echo "run flash_reset_env" > "$1"
sleep 1
echo "setenv extrabootargs \"quiet\"; setenv bootdelay 0; setenv bootcmd \"run loadbootscript; run bootscript;\"; setenv bootscript \"echo running bootscript; source \${scriptaddr}\"; saveenv" > "$1"
sleep 1
echo "Done"
}
function startUMS
{
echo " " > "$1"
echo " " > "$1"
echo "ums 0 mmc 0" > "$1"
}
function stopUMS
{
expect -c "send \003;" > "$1"
}
function detectUMS
{
sleep 3
umsDisk=$(lsblk -o NAME,MODEL | grep "UMS disk" | cut -d' ' -f1)
nbUms=$(lsblk -o NAME,MODEL | grep "UMS disk" | cut -d' ' -f1 | wc -l)
#nbUms=$(echo -n "$umsDisk"| wc -l)
if [ "$nbUms" != "1" ]; then
>&2 echo "Cannot auto detect disk, exiting"
exit 1
fi
echo "$umsDisk"
}
function flashDiskNet
{
local tty="$1"
local images="$2"
if [ "$tty" == "" ]; then
echo "Flash disk by eth need tty option. Giving Up"
exit 1
fi
echo "images to flash $images"
echo "setenv ipaddr 192.168.0.100; setenv serverip ${THIS_COMPUTER_IP}" > "$tty"
sleep 0.5
for desc in $images; do
image=$(echo "$desc" | cut -d';' -f1)
part=$(echo "$desc" | cut -d';' -f2)
crc_file=$(crc32 "$image" | cut -f1)
echo "Flashing $image with CRC ${crc_file}"
case "$part" in
1)
cp "$image" $TFTP_SERVER_PATH/apf6-boot.ext4
echo "run update_boot" > "$tty"
waitForText "$tty" "Flashing of boot image succeed" > /dev/null
echo "mmc read \${loadaddr} 0x800 \${nbblocks}; crc32 \${loadaddr} \${filesize}" > "$tty"
;;
2)
cp "$image" $TFTP_SERVER_PATH/apf6-rootfs.ext4
echo "run update_rootfs" > "$tty"
waitForText "$tty" "Flashing of rootfs image succeed" > /dev/null
echo "mmc read \${loadaddr} 0x18800 \${nbblocks}; crc32 \${loadaddr} \${filesize}" > "$tty"
;;
3)
cp "$image" $TFTP_SERVER_PATH/$UBOOT_IMG_NAME
echo "run download_uboot_img flash_uboot_img" > "$tty"
waitForText "$tty" "Flashing of" > /dev/null
echo "mmc read \${loadaddr} 0x8a \${nbblocks}; crc32 \${loadaddr} \${filesize}" > "$tty"
;;
4)
cp "$image" $TFTP_SERVER_PATH/$UBOOT_SPL_NAME
echo "run download_uboot_spl flash_uboot_spl" > "$tty"
waitForText "$tty" "Flashing of" > /dev/null
echo "mmc read \${loadaddr} 0x2 \${nbblocks}; crc32 \${loadaddr} \${filesize}" > "$tty"
;;
*)
echo "Unsupported partition $part"
exit 1
;;
esac
crc_computed=$(waitForText "$tty" "CRC32 for" | awk -F "==>" '{print $2}' | tr -d '[:space:]')
echo "Computed checksum ${crc_computed}"
if [ "$crc_computed" != "$crc_file" ]; then
echo -e "${RED}CHECKSUM ERROR${NC} for $image (got \"$crc_computed\" but should be \"$crc_file\")"
fi
echo "done"
done
}
function flashDisk
{
local tty="$1"
local images="$2"
echo "images to flash $images"
if [ "$tty" != "" ]; then
echo "Start UMS"
startUMS "$tty"
fi
if [ "$disk" == "" ]; then
disk=$(detectUMS)
fi
if [ "$disk" == "" ]; then
>&2 echo "Cannot found disk, exiting"
exit 1
fi
if [ ! -e "/dev/$disk" ]; then
>&2 echo "disk /dev/$disk not found. Giving up"
exit 1
fi
for desc in $images; do
image=$(echo "$desc" | cut -d';' -f1)
part=$(echo "$desc" | cut -d';' -f2)
diskPath="/dev/${disk}${part}"
echo "flashing $image on $diskPath"
if [ ! -e "$image" ]; then
>&2 echo "$image cannot be found"
exit 1
fi
if grep -qs $diskPath /proc/mounts; then
echo "$diskPath mounted ! unmouting it"
echo "Is disk automounted ? try to run \"gsettings set org.gnome.desktop.media-handling automount false\" to disable it"
umount $diskPath
fi
sudo dd if="$image" of="$diskPath" bs=4M status=progress oflag=direct
# flush file system buffers. Force changed blocks to disk, update the super block.
sync
done
if [ "$tty" != "" ]; then
stopUMS "$tty"
sleep 0.5
for desc in $images; do
image=$(echo "$desc" | cut -d';' -f1)
part=$(echo "$desc" | cut -d';' -f2)
crc_file=$(crc32 "$image" | cut -f1)
size=$(du -b "$image" | cut -f1)
size_hex=$(printf "%x" "$size")
part_addr=0
echo "Computing flashed $image CRC ${crc_file} of size 0x${size_hex}"
nbblocks=$(( 0x$size_hex / $BLOCK_SIZE + 1))
nbblocks=$(printf "%x" "$nbblocks")
if [ "$part" == "1" ]; then
part_addr=0x800
else
part_addr=0x18800
fi
echo "mmc read \${loadaddr} ${part_addr} 0x${nbblocks}; crc32 \${loadaddr} 0x${size_hex}" > "$tty"
crc_computed=$(waitForText "$tty" "CRC32 for" | awk -F "==>" '{print $2}' | tr -d '[:space:]')
echo "Computed checksum ${crc_computed}"
if [ "$crc_computed" != "$crc_file" ]; then
echo -e "${RED}CHECKSUM ERROR${NC} for $image (got $crc_computed but should be $crc_file)"
exit 1
fi
done
else
echo "no tty given -> cannot computed CRC"
fi
if [ "$tty" != "" ]; then
echo "Restart board for first boot"
echo "reset" > "$tty"
fi
# wait for first boot to complete
sleep 30
}
echo "Version: $VERSION"
while getopts "afb:s:d:t:u:" opt; do
case $opt in
a) echo "Flashing all images"
;;
b) echo "Flashing boot image ${OPTARG}"
images="${images} ${OPTARG};1"
;;
s) echo "Flashing system image ${OPTARG}"
images="${images} ${OPTARG};2"
;;
d) echo "Using disk $OPTARG"
disk=$OPTARG
;;
t) echo "Using tty $OPTARG"
tty=$OPTARG
;;
u) echo "Flashing uboot $OPTARG"
ubootimg=$OPTARG
;;
f) echo "use UMS for flashing"
ums=1
;;
\?)
echo "Invalid option: -$opt" >&2
exit 1
;;
esac
done
if [ "$tty" == "" ] && [ "$ubootimg" != "" ]; then
>&2 echo "TTY should be provided when flashing uboot"
exit 1
fi
if [ "$tty" != "" ]; then
echo "Please reboot the board"
enterUboot "$tty"
fi
if [ "$ubootimg" != "" ]; then
flash_uboot "$tty" "$ubootimg"
fi
if [ "$images" != "" ]; then
if [ $ums == 1 ]; then
flashDisk "$tty" "$images"
else
flashDiskNet "$tty" "$images"
fi
fi
echo "SOM programmation done"
notify-send --urgency=low "Flashing PRIMA2 finished"

View File

@ -1,5 +1,6 @@
#!/bin/bash
tmpfile=$(mktemp /tmp/img-diff.XXXXXX)
compare -compose src $1 $2 $tmpfile
compare -compose src -metric AE $1 $2 $tmpfile
#compare -metric PSNR $1 $2 $tmpfile
eog -n $tmpfile
rm $tmpfile

13
scripts/md2docx Executable file
View File

@ -0,0 +1,13 @@
#!/bin/bash
file=$1
extra=""
shift
if [ -e listings-setup.tex ]; then
extra=" --listings -H listings-setup.tex "
fi
if [ -e custom-reference.docx ]; then
extra="$extra --reference-docx=custom-reference.docx "
fi
pandoc --toc "${file}" -o "${file%.*}.docx" --number-sections --variable urlcolor=cyan ${extra} $@

View File

@ -2,8 +2,9 @@
file=$1
extra=""
shift
if [ -e listing-setup.tex ]; then
extra=" --listings -H listing-setup.tex "
if [ -e listings-setup.tex ]; then
extra=" --listings -H listings-setup.tex "
fi
pandoc -s -S ${file} --toc -o ${file%.*}.pdf --variable urlcolor=cyan ${extra} $@
pandoc -s -S "${file}" --toc -o "${file%.*}.pdf" --number-sections --variable urlcolor=cyan ${extra} $@ #--latex-engine=xelatex

3
scripts/speech.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash
say() { local IFS=+;/usr/bin/mplayer -ao alsa -really-quiet -noconsolecontrols "http://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&q=$*&tl=en"; }
say $*

16
scripts/tag_add.sh Executable file
View File

@ -0,0 +1,16 @@
#!/bin/bash
REMOTE=$(git remote)
BRANCH=$(git branch | grep '*' | sed 's/* //')
TAG=$1
MSG=$2
if [ $# -eq 0 ]; then
echo 'Missing parameters'
exit 0
fi
git tag -a $TAG -m "$MSG"
git push $REMOTE --tags
exit 0

14
scripts/tag_cmp.sh Executable file
View File

@ -0,0 +1,14 @@
#!/bin/bash
TAG1=$1
TAG2=$2
if [ $# -eq 0 ]; then
echo 'Missing parameters'
exit 0
fi
#git log --decorate --abbrev-commit --no-merges --format="%C(auto) %h %s" "$TAG1".."$TAG2"
git log --decorate --no-merges "$TAG1".."$TAG2"
exit 0