Skip to content

Commit

Permalink
Add Android flash scripts
Browse files Browse the repository at this point in the history
The scripts are used to flash Android VM in SOS

Test Done:
flash android and boot

Tracked-On: OAM-123554
Signed-off-by: Chen, Gang G <[email protected]>
  • Loading branch information
GangSecurity committed Aug 22, 2024
1 parent 2a35fd7 commit 38598e3
Show file tree
Hide file tree
Showing 5 changed files with 343 additions and 0 deletions.
77 changes: 77 additions & 0 deletions flash_scripts/android_flash_base_aaos.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Copyright (C) 2024 Intel Corporation
# Author: Chen, Gang G <[email protected]>
# SPDX-License-Identifier: BSD-3-Clause

#!/bin/sh

ZIP_FILE=$(readlink -f $1)
VM_IMG=$(readlink -f $2)
EXTRACT_DISK=/dev/nvme0n1p5

usage()
{
echo $1
echo "Usage: $0 <flashfiles>.zip <disk_name>"
echo "disk_name can be partition(/dev/nvme0n1pX) or virtual_disk(/<path>/aaos.img)"
echo "If you want to use virtual_disk(/home/root/aaos.img), please create it in advance"
echo "For example: create a 400GB image. please make sure your disk has enough space"
echo "command: fallocate -l 400G /<path>/aaos.img"
}

if [ $# != 2 ]; then
usage "Invalid arguments"
exit 1
fi

if [ ! -f $ZIP_FILE ]; then
usage "$ZIP_FILE not exist"
exit 1
fi

if [ ! -e $VM_IMG ]; then
usage "$VM_IMG not exist"
exit 1
fi

echo "You are going to create image ($VM_IMG) for VM"
echo "You are going to format ($EXTRACT_DISK) as temporary extraction location"
echo "Please confirm that output image paths are correct !"
read -p "Continue ? [y/n]" yn
case $yn in
[Yy]*) ;;
[Nn]*) exit;;
esac

mkfs.ext4 $VM_IMG -F

if [ ! -e $EXTRACT_DISK ]; then
usage "$EXTRACT_DISK not exist"
exit 1
fi

mkfs.ext4 $EXTRACT_DISK -F

TEMP_DIR=/home/root/image

rm -rf $TEMP_DIR
mkdir -p $TEMP_DIR
mount $EXTRACT_DISK $TEMP_DIR

cd $TEMP_DIR
case $ZIP_FILE in
*.zip) 7z x $ZIP_FILE;;
*.tar.gz) tar -xvzf $ZIP_FILE;;
*) echo "Please make sure the image suffix is ".gz" or "tar.gz"; exit"
exit 0
;;
esac

simg2img super.img super.img.raw
mv super.img.raw super.img
simg2img config.img config.img.raw
mv config.img.raw config.img
cd -

SIZE_GB=$(fdisk -l $VM_IMG | grep Disk | awk '{print $3}')
SIZE_GB=$(awk "BEGIN {print int($SIZE_GB)}")
python3 ./create_gpt_image.py --create $VM_IMG --size=${SIZE_GB}G --flashfiles $TEMP_DIR
87 changes: 87 additions & 0 deletions flash_scripts/android_flash_vm1_2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Copyright (C) 2024 Intel Corporation
# Author: Chen, Gang G <[email protected]>
# SPDX-License-Identifier: BSD-3-Clause

#!/bin/sh

usage()
{
echo $1
echo "Usage: $0 <flashfiles>.zip <vm_ro.img> <vm1_rw.img> <vm2_rw.img>"
}

if [ $# != 4 ]; then
usage "Invalid arguments"
exit 1
fi

ZIP_FILE=$(readlink -f $1)
VM_RO_IMG=$(readlink -f $2)
VM1_RW_IMG=$(readlink -f $3)
VM2_RW_IMG=$(readlink -f $4)

if [ ! -f $ZIP_FILE ]; then
usage "$ZIP_FILE not exist"
exit 1
fi

echo "You are going to create a readonly image ($VM_RO_IMG) for both VM1 and VM2"
echo "and two read-write images ($VM1_RW_IMG and $VM2_RW_IMG) for VM1 and VM2 separately"
echo "Please confirm that output image paths are correct !"
read -p "Continue ? [y/n]" yn
case $yn in
[Yy]*) ;;
[Nn]*) exit;;
esac

mkfs.ext4 $VM_RO_IMG -F
mkfs.ext4 $VM1_RW_IMG -F
mkfs.ext4 $VM2_RW_IMG -F
TEMP_DIR=$(mktemp -d)
pushd $TEMP_DIR
7z x $ZIP_FILE
simg2img super.img super.img.raw
mv super.img.raw super.img
simg2img config.img config.img.raw
mv config.img.raw config.img
popd

GPT_INI=/home/root/android-flashtool/gpt.ini
if [ -f $TEMP_DIR/gpt.ini ]; then
GPT_INI=$TEMP_DIR/gpt.ini
echo "Use gpt.ini in flashfiles: $GPT_INI"
elif [ -f $GPT_INI ]; then
echo "Use default gpt.ini: $GPT_INI"
else
echo "No gpt.ini found! exit!"
exit 0
fi

TEMP_INI=$(mktemp)
sed -e '/partitions =/s/teedata //g' $GPT_INI >$TEMP_INI
sed -e '/partitions =/s/share_data//g' -e '/partitions =/s/metadata //g' -e '/partitions =/s/persistent //g' -e '/partitions =/s/data //g' -i $TEMP_INI
SIZE_GB=$(fdisk -l $VM_RO_IMG | grep Disk | awk '{print $3}')
SIZE_GB=$(awk "BEGIN {print int($SIZE_GB)}")
python3 ./gpt_ini2bin.py $TEMP_INI > ${TEMP_DIR}/gpt.bin
python3 ./create_gpt_image.py --create $VM_RO_IMG --size=${SIZE_GB}G --flashfiles $TEMP_DIR

cp $GPT_INI $TEMP_INI
sed -e '/partitions =/s/bootloader //g' -e '/partitions =/s/boot //g' -e '/partitions =/s/misc //g' -i $TEMP_INI
sed -e '/partitions =/s/acpio //g' -e '/partitions =/s/super //g' -e '/partitions =/s/vbmeta //g' -i $TEMP_INI
sed -e '/partitions =/s/share_data//g' -i $TEMP_INI #vm1/2 only
sed -e 's/partitions += vendor_boot//' -i $TEMP_INI
sed -e 's/partitions += config//' -i $TEMP_INI
sed -e 's/len = 128000/len = -1/' -i $TEMP_INI #vm1/2 only

SIZE_GB=$(fdisk -l $VM1_RW_IMG | grep Disk | awk '{print $3}')
SIZE_GB=$(awk "BEGIN {print int($SIZE_GB)}")
python3 ./gpt_ini2bin.py $TEMP_INI > ${TEMP_DIR}/gpt.bin
python3 ./create_gpt_image.py --create $VM1_RW_IMG --size=${SIZE_GB}G --flashfiles $TEMP_DIR

SIZE_GB=$(fdisk -l $VM2_RW_IMG | grep Disk | awk '{print $3}')
SIZE_GB=$(awk "BEGIN {print int($SIZE_GB)}")
python3 ./gpt_ini2bin.py $TEMP_INI > ${TEMP_DIR}/gpt.bin
python3 ./create_gpt_image.py --create $VM2_RW_IMG --size=${SIZE_GB}G --flashfiles $TEMP_DIR

rm -rf ${TEMP_DIR}/*
sync
77 changes: 77 additions & 0 deletions flash_scripts/android_flash_vm3.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Copyright (C) 2024 Intel Corporation
# Author: Chen, Gang G <[email protected]>
# SPDX-License-Identifier: BSD-3-Clause

#!/bin/sh

usage()
{
echo $1
echo "Usage: $0 <flashfiles>.zip <vm3_ro.img> <vm3_rw.img>"
}

if [ $# != 3 ]; then
usage "Invalid arguments"
exit 1
fi

ZIP_FILE=$(readlink -f $1)
VM_RO_IMG=$(readlink -f $2)
VM3_RW_IMG=$(readlink -f $3)

if [ ! -f $ZIP_FILE ]; then
usage "$ZIP_FILE not exist"
exit 1
fi

echo "You are going to create image ($VM_RO_IMG) for VM1 and image($VM3_RW_IMG ) for VM3"
echo "Please confirm that output image paths are correct !"
read -p "Continue ? [y/n]" yn
case $yn in
[Yy]*) ;;
[Nn]*) exit;;
esac

mkfs.ext4 $VM_RO_IMG -F
mkfs.ext4 $VM3_RW_IMG -F
TEMP_DIR=$(mktemp -d)
pushd $TEMP_DIR
7z x $ZIP_FILE
simg2img super.img super.img.raw
mv super.img.raw super.img
simg2img config.img config.img.raw
mv config.img.raw config.img
popd

GPT_INI=/home/root/android-flashtool/gpt.ini
if [ -f $TEMP_DIR/gpt.ini ]; then
GPT_INI=$TEMP_DIR/gpt.ini
echo "Use gpt.ini in flashfiles: $GPT_INI"
elif [ -f $GPT_INI ]; then
echo "Use default gpt.ini: $GPT_INI"
else
echo "No gpt.ini found! exit!"
exit 0
fi

TEMP_INI=$(mktemp)
sed -e '/partitions =/s/teedata //g' $GPT_INI >$TEMP_INI
sed -e '/partitions =/s/share_data//g' -e '/partitions =/s/metadata //g' -e '/partitions =/s/persistent //g' -e '/partitions =/s/data //g' -i $TEMP_INI
SIZE_GB=$(fdisk -l $VM_RO_IMG | grep Disk | awk '{print $3}')
SIZE_GB=$(awk "BEGIN {print int($SIZE_GB)}")
python3 ./gpt_ini2bin.py $TEMP_INI > ${TEMP_DIR}/gpt.bin
python3 ./create_gpt_image.py --create $VM_RO_IMG --size=${SIZE_GB}G --flashfiles $TEMP_DIR

cp $GPT_INI $TEMP_INI
sed -e '/partitions =/s/bootloader //g' -e '/partitions =/s/boot //g' -e '/partitions =/s/misc //g' -i $TEMP_INI
sed -e '/partitions =/s/acpio //g' -e '/partitions =/s/super //g' -e '/partitions =/s/vbmeta //g' -i $TEMP_INI
sed -e 's/partitions += vendor_boot//' -i $TEMP_INI
sed -e 's/partitions += config//' -i $TEMP_INI

SIZE_GB=$(fdisk -l $VM3_RW_IMG | grep Disk | awk '{print $3}')
SIZE_GB=$(awk "BEGIN {print int($SIZE_GB)}")
python3 ./gpt_ini2bin.py $TEMP_INI > ${TEMP_DIR}/gpt.bin
python3 ./create_gpt_image.py --create $VM3_RW_IMG --size=${SIZE_GB}G --flashfiles $TEMP_DIR

rm -rf ${TEMP_DIR}/*
sync
56 changes: 56 additions & 0 deletions flash_scripts/flash_vm1_2_rw.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright (C) 2024 Intel Corporation
# Author: Chen, Gang G <[email protected]>
# SPDX-License-Identifier: BSD-3-Clause

#!/bin/sh
# The script will format vm1 and vm2 rw partitions and flash GPT table to them

usage()
{
echo $1
echo "Usage: $0 <vm1_rw.img> <vm2_rw.img>"
}

if [ $# != 2 ]; then
usage "Invalid arguments"
exit 1
fi

VM1_RW_IMG=$(readlink -f $1)
VM2_RW_IMG=$(readlink -f $2)

echo "You are going to create two read-write images ($VM1_RW_IMG and $VM2_RW_IMG) for VM1 and VM2 separately"
echo "Please confirm that output image paths are correct !"
read -p "Continue ? [y/n]" yn
case $yn in
[Yy]*) ;;
[Nn]*) exit;;
esac

mkfs.ext4 $VM1_RW_IMG -F
mkfs.ext4 $VM2_RW_IMG -F


TEMP_DIR=$(mktemp -d)

TEMP_INI=$(mktemp)
rm -rf ${TEMP_DIR}/*
cp gpt.ini $TEMP_INI
sed -e '/partitions =/s/bootloader //g' -e '/partitions =/s/boot //g' -e '/partitions =/s/misc //g' -i $TEMP_INI
sed -e '/partitions =/s/acpio //g' -e '/partitions =/s/super //g' -e '/partitions =/s/vbmeta //g' -i $TEMP_INI
sed -e '/partitions =/s/share_data//g' -i $TEMP_INI #vm1/2 only
sed -e 's/partitions += vendor_boot//' -i $TEMP_INI
sed -e 's/partitions += config//' -i $TEMP_INI
sed -e 's/len = 128000/len = -1/' -i $TEMP_INI #vm1/2 only

SIZE_GB=$(fdisk -l $VM1_RW_IMG | grep Disk | awk '{print $3}')
SIZE_GB=$(awk "BEGIN {print int($SIZE_GB)}")
python3 ./gpt_ini2bin.py $TEMP_INI > ${TEMP_DIR}/gpt.bin
python3 ./create_gpt_image.py --create $VM1_RW_IMG --size=${SIZE_GB}G --flashfiles $TEMP_DIR

SIZE_GB=$(fdisk -l $VM2_RW_IMG | grep Disk | awk '{print $3}')
SIZE_GB=$(awk "BEGIN {print int($SIZE_GB)}")
python3 ./gpt_ini2bin.py $TEMP_INI > ${TEMP_DIR}/gpt.bin
python3 ./create_gpt_image.py --create $VM2_RW_IMG --size=${SIZE_GB}G --flashfiles $TEMP_DIR

sync
46 changes: 46 additions & 0 deletions flash_scripts/flash_vm3_rw.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright (C) 2024 Intel Corporation
# Author: Chen, Gang G <[email protected]>
# SPDX-License-Identifier: BSD-3-Clause

#!/bin/sh
# the script will format vm3 RW partition and flash GPT table to it

usage()
{
echo $1
echo "Usage: $0 <vm3_rw.img>"
}

if [ $# != 1 ]; then
usage "Invalid arguments"
exit 1
fi

VM3_RW_IMG=$(readlink -f $1)

echo "You are going to create image ($VM3_RW_IMG ) for VM3"
echo "Please confirm that output image paths are correct !"
read -p "Continue ? [y/n]" yn
case $yn in
[Yy]*) ;;
[Nn]*) exit;;
esac

mkfs.ext4 $VM3_RW_IMG -F

TEMP_DIR=$(mktemp -d)
TEMP_INI=$(mktemp)

rm -rf ${TEMP_DIR}/*
cp gpt.ini $TEMP_INI
sed -e '/partitions =/s/bootloader //g' -e '/partitions =/s/boot //g' -e '/partitions =/s/misc //g' -i $TEMP_INI
sed -e '/partitions =/s/acpio //g' -e '/partitions =/s/super //g' -e '/partitions =/s/vbmeta //g' -i $TEMP_INI
sed -e 's/partitions += vendor_boot//' -i $TEMP_INI
sed -e 's/partitions += config//' -i $TEMP_INI

SIZE_GB=$(fdisk -l $VM3_RW_IMG | grep Disk | awk '{print $3}')
SIZE_GB=$(awk "BEGIN {print int($SIZE_GB)}")
python3 ./gpt_ini2bin.py $TEMP_INI > ${TEMP_DIR}/gpt.bin
python3 ./create_gpt_image.py --create $VM3_RW_IMG --size=${SIZE_GB}G --flashfiles $TEMP_DIR

sync

0 comments on commit 38598e3

Please sign in to comment.