-
Notifications
You must be signed in to change notification settings - Fork 4
/
rpk_update_network_intrinsics
executable file
·120 lines (104 loc) · 2.69 KB
/
rpk_update_network_intrinsics
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
#!/bin/sh
# Deps:
# - binwalk
# - sed
# - dd
# - jq
# - truncate (coreutils)
# - stat (coreutils)
# Description:
# rpk files are often used in conjuction with imx500 python demo scripts.
# When used in this way, it is important to select the correct script for the
# task (i.e. classification, segmentation, etc.). Certain networks also
# require other paramaters to be set (such as fps) to achieve optimal
# performance.
#
# This script is able to update/add network_intrinsics to an .rpk file.
#
# e.g:
# update_network_intrinsics <RPK file> <JSON intrinsics file>
#
# A JSON intrinsics file can be produced by invoking a demo-script with the
# desired arguments with the addition of '--print-intrinsics'
set -e
if [ "$#" -ne 2 ]
then
>&2 echo "Usage: update_network_intrinsics <RPK file> <JSON intrinsics file>"
exit 1
fi
RPK_FILE="$(realpath "$1")"
INT_FILE="$(realpath "$2")"
if [ ! -f "$RPK_FILE" ] || [ ! -f "$INT_FILE" ]
then
>&2 echo "Usage: update_network_intrinsics <RPK file> <JSON intrinsics file>"
exit 1
fi
# Deps: binwalk / sed
calc_cpio_offset() {
binwalk "$1" | \
sed \
--quiet \
--regexp-extended \
'/ASCII cpio archive/{s/^([[:digit:]]+).*/\1/; p; q}'
}
# Calculate offset of CPIO within .rpk file using binwalk
CPIO_OFFSET="$(calc_cpio_offset "$RPK_FILE")"
if [ -z "$CPIO_OFFSET" ]
then
>&2 echo "ERROR: Could not calculate CPIO offset"
exit 1
fi
# Use dd to extract CPIO archive
CPIO_ARCHIVE="$(mktemp)"
2>/dev/null \
dd \
ibs=1 \
iseek="$CPIO_OFFSET" \
if="$RPK_FILE" \
of="$CPIO_ARCHIVE"
# Get order of files in CPIO file
FILE_LIST="$(mktemp)"
2>/dev/null \
cpio \
--file "$CPIO_ARCHIVE" \
-t \
> "$FILE_LIST"
# Append 'network_intrinsics' to file list if not already present
grep \
--quiet \
--line-regexp \
--fixed-strings \
network_intrinsics \
"$FILE_LIST" || \
echo network_intrinsics >> "$FILE_LIST"
CPIO_EXTRACT_DIR="$(mktemp -d)"
cd "$CPIO_EXTRACT_DIR"
2>/dev/null \
cpio \
--file "$CPIO_ARCHIVE" \
--extract \
--preserve-modification-time
cp "$INT_FILE" ./network_intrinsics
touch -t 314103141031.41 network_intrinsics
2>/dev/null \
cpio \
--create \
--format=newc \
--reproducible \
--reset-access-time \
< "$FILE_LIST" \
> "$CPIO_ARCHIVE"
cd - >/dev/null
rm -f "$FILE_LIST"
rm -rf "$CPIO_EXTRACT_DIR"
# Use dd to overwrite CPIO archive in rpk file
2>/dev/null \
dd \
conv=notrunc \
obs=1 \
oseek="$CPIO_OFFSET" \
of="$RPK_FILE" \
if="$CPIO_ARCHIVE"
# Truncate the RPK file (needed in case it shrunk)
truncate --no-create --size=$((CPIO_OFFSET + $(stat --format='%s' "$CPIO_ARCHIVE"))) "$RPK_FILE"
rm "$CPIO_ARCHIVE"