~aleteoryx/muditaos

muditaos/tools/generate_image.sh -rwxr-xr-x 3.3 KiB
a405cad6Aleteoryx trim readme 6 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash -e
# Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

usage() {
cat << ==usage
Usage: $(basename $0) image_path image_partitions sysroot
    image_path        - Destination image path name e.g., PurePhone.img
    image_partitions  - Path to image_partitions.map product-specific file
    sysroot           - product's system root e.g., build-rt1051-RelWithDebInfo/sysroot
==usage
}

if [[ ( $# -ne 3 ) ]]; then
	echo "Error! Invalid argument count $# args: $*"
	usage
	exit 1
fi

IMAGE_NAME=$(realpath $1)
IMAGE_PARTITIONS=$(realpath $2)
SYSROOT=$(realpath $3)

if [ ! -d "$SYSROOT" ]; then
	echo "Error! ${SYSROOT} is not a directory"
	usage
	exit 1
fi
_REQ_CMDS="sfdisk truncate mke2fs"
for cmd in $_REQ_CMDS; do
	if [ ! $(command -v $cmd) ]; then
		echo "Error! $cmd is not installed, please use 'sudo apt install' for install required tool"
		exit 1
	fi
done

source ${IMAGE_PARTITIONS}
DEVICE_BLK_SIZE=512

# Partition sizes in sector 512 units
PART1_START=1
PART2_START=$(($PART1_START + $PART1_SIZE))
PART2_SIZE=$PART1_SIZE
PART3_START=$(($PART2_START + $PART2_SIZE))
PART3_SIZE=$(($DEVICE_BLK_COUNT - $PART1_SIZE - $PART2_SIZE - $PART1_START))

# Remove previous file
echo "Removing previous image file..."
rm -f $IMAGE_NAME
# Create image file and set its length to block device size
truncate -s $(($DEVICE_BLK_COUNT * $DEVICE_BLK_SIZE)) $IMAGE_NAME

# Create image
echo "Creating empty disk image..."
sfdisk $IMAGE_NAME << ==sfdisk
label: dos
label-id: 0x09650eb4
unit: sectors

/dev/sdx1 : start=    $PART1_START,  size=    $PART1_SIZE, type=83
/dev/sdx2 : start=    $PART2_START,  size=    $PART2_SIZE, type=83
/dev/sdx3 : start=    $PART3_START,  size=    $PART3_SIZE, type=83
==sfdisk

# Validate image source folder
if [ ! -d "${SYSROOT}/system_a" ]; then
	echo "Fatal! Image folder 'system_a' missing in build. Check build system."
	exit 1
fi
if [ ! -d "${SYSROOT}/user" ]; then
	echo "Fatal! Image folder 'user' missing in build. Check build system."
	exit 1
fi
cd "${SYSROOT}"

# Format partitions
# Disabling of the below options is required:
# ^64bit - 64-bit inodes are not supported by lwext4
# ^metadata_csum - metadata checksums have buggy implementation in lwext4

# Format ext4 system_a partition
echo "Formatting system_a partition as ext4..."
mke2fs \
  -F \
  -L 'system_a' \
  -N 0 \
  -E offset=$(($PART1_START*$DEVICE_BLK_SIZE)) \
  -O ^64bit \
  -O ^flex_bg \
  -O ^metadata_csum \
  -m 0 \
  -r 1 \
  -d 'system_a' \
  -t ext4 \
  "$IMAGE_NAME" \
  $((($PART1_SIZE*$DEVICE_BLK_SIZE)/1024)) > /dev/null

# Format ext4 system_b partition
echo "Formatting system_b partition as ext4..."
mke2fs \
  -F \
  -L 'system_b' \
  -N 0 \
  -E offset=$(($PART2_START*$DEVICE_BLK_SIZE)) \
  -O ^64bit \
  -O ^flex_bg \
  -O ^metadata_csum \
  -m 0 \
  -r 1 \
  -t ext4 \
  "$IMAGE_NAME" \
  $((($PART2_SIZE*$DEVICE_BLK_SIZE)/1024)) > /dev/null

# Format ext4 user partition
echo "Formatting user partition as ext4..."
mke2fs \
  -F \
  -L 'user' \
  -N 0 \
  -E offset=$(($PART3_START*$DEVICE_BLK_SIZE)) \
  -O ^64bit \
  -O ^flex_bg \
  -O ^metadata_csum \
  -m 0 \
  -r 1 \
  -d 'user' \
  -t ext4 \
  "$IMAGE_NAME" \
  $((($PART3_SIZE*$DEVICE_BLK_SIZE)/1024)) > /dev/null

echo "Image created!"

# Back to previous dir
cd - > /dev/null
sync