-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild_uce.sh
executable file
·137 lines (95 loc) · 3.96 KB
/
build_uce.sh
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
136
#!/bin/bash
set -e
if (($# < 2)); then
echo "Usage:"
echo $0 /path/to/inputdata output.uce
exit 1
fi
inputdir="$1"
cart_file=$2
mkdir -p out
workdir=$(mktemp -d)
trap "rm -R $workdir" 0 2 3 15
export BASEDIR="$PWD"
cart_tmp_file="$workdir/byog_cartridge_shfs_temp.img"
cart_save_file="$workdir/byog_cart_saving_ext4.img"
my_md5string_hex_file="$workdir/my_md5string_hex.bin"
cart_saving_size=4M
/bin/rm -f $cart_tmp_file
/bin/rm -f $cart_save_file
mkdir -p "$workdir/data"
rsync -a "$inputdir"/ "$workdir/data/"
# create the save partition
mkdir -p "$workdir/save/upper"
mkdir -p "$workdir/save/work"
if [ -d "$workdir/data/save" ]; then
# find "$workdir/data/save" -type f -exec chmod 666 {} \;
find "$workdir/data/save" -type d -exec chmod 777 {} \;
find "$workdir/data/save" -type f -exec chmod a+rw {} \;
#/bin/mv "$workdir/data/save"/* "$workdir/save/upper"
rsync -a "$workdir/data/save"/ "$workdir/save/upper"/
/bin/rm -rf "$workdir/data/save"
fi
mkdir -p "$workdir/data/save"
truncate -s $cart_saving_size $cart_save_file
mkfs.ext4 -d "$workdir/save" $cart_save_file
find "$workdir/data" -name "*.lnk" -type f -print0 | xargs -0 -n1 ./resolve.sh
find "$workdir/data" -name "*.lnk" -type f -delete
chmod 755 "$workdir/data/exec.sh"
# resize box art
# convert "$1"/boxart/boxart.* -resize 223x307 boxart_resized.png
# /bin/rm -f "$1"/boxart/boxart.*
# /bin/mv boxart_resized.png "$1"/boxart/boxart.png
$(cd "$workdir/data" && rm -f title.png && ln -sf boxart/boxart.png title.png)
if [ -f "$workdir/data/save.zip" ]
then
/bin/mv "$workdir/data/save.zip" "$workdir/"
fi
mksquashfs "$workdir/data" $cart_tmp_file -b 262144 -root-owned -nopad
SQIMGFILESIZE=$(stat -c%s "$cart_tmp_file")
echo "*** Size of $cart_tmp_file: $SQIMGFILESIZE Bytes (before applying 4k alignment)"
REAL_BYTES_USED_DIVIDED_BY_4K=$((SQIMGFILESIZE/4096))
if [ $((SQIMGFILESIZE % 4096)) != 0 ]
then
REAL_BYTES_USED_DIVIDED_BY_4K=$((REAL_BYTES_USED_DIVIDED_BY_4K+1))
fi
REAL_BYTES_USED=$((REAL_BYTES_USED_DIVIDED_BY_4K*4096))
dd if=/dev/zero bs=1 count=$((REAL_BYTES_USED-SQIMGFILESIZE)) >> $cart_tmp_file
SQIMGFILESIZE=$(stat -c%s "$cart_tmp_file")
echo "*** Size of $cart_tmp_file: $SQIMGFILESIZE Bytes (after applying 4k alignment)"
# header padding 64 bytes
EXT4FILE_OFFSET=$((SQIMGFILESIZE+64));
echo "*** Offset of Ext4 partition for file saving would be: $EXT4FILE_OFFSET ($SQIMGFILESIZE + 64)"
md5=$(md5sum "$cart_tmp_file" | cut -d ' ' -f 1)
echo "*** SQFS Partition MD5 Hash: "$md5""
echo $md5 | xxd -r -p > $my_md5string_hex_file
echo "*** Appending 16 bytes of SQFS Partition MD5 to $cart_tmp_file"
dd if=$my_md5string_hex_file of=$cart_tmp_file ibs=16 count=1 obs=16 oflag=append conv=notrunc
filesize=$(stat -c%s "$cart_tmp_file")
echo "*** Size of $cart_tmp_file: $filesize Bytes (after SQFS MD5)"
echo "*** Padding 32 bytes of zeros to $cart_tmp_file"
dd if=/dev/zero of=$cart_tmp_file ibs=16 count=2 obs=16 oflag=append conv=notrunc
filesize=$(stat -c%s "$cart_tmp_file")
echo "*** Size of $cart_tmp_file: $filesize Bytes (after padding zeros)"
/bin/rm -f $my_md5string_hex_file
md5=$(md5sum "$cart_save_file" | cut -d ' ' -f 1)
echo "*** Ext4 Partition MD5 Hash: "$md5""
echo $md5 | xxd -r -p > $my_md5string_hex_file
echo "*** Appending 16 bytes of Ext4 Partition MD5 to $cart_tmp_file"
dd if=$my_md5string_hex_file of=$cart_tmp_file ibs=16 count=1 obs=16 oflag=append conv=notrunc
filesize=$(stat -c%s "$cart_tmp_file")
echo "*** Size of $cart_tmp_file: $filesize Bytes (after Ext4 MD5)"
filesize=$(stat -c%s "$cart_save_file")
echo "*** Size of $cart_save_file: $filesize Bytes (save partition)"
#bind files together
if [ -f "$workdir/save.zip" ]
then
unzip "$workdir/save.zip" -d "$workdir"
/bin/mv "$workdir/save.bin" $cart_save_file
fi
cat $cart_tmp_file $cart_save_file > "$cart_file"
filesize=$(stat -c%s "$cart_file")
echo "*** Final size ($cart_file): $filesize Bytes (cart + save)"
#/bin/rm -f $my_md5string_hex_file
#/bin/rm -f $cart_tmp_file
#/bin/rm -f $cart_save_file