forked from SPRESENSE/nuttx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkromfsimg.sh
executable file
·312 lines (249 loc) · 9.29 KB
/
mkromfsimg.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env bash
############################################################################
# tools/mkromfsimg.sh
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################
# Environmental stuff
wd=`pwd`
workingdir=$wd/img
rcsysinitfile=rc.sysinit
rcsysinittemplate=$rcsysinitfile.template
rcsfile=rcS
rcstemplate=$rcsfile.template
romfsimg=romfs.img
headerfile=nsh_romfsimg.h
# Get the input parameters
nofat=$1
usefat=true
topdir=$2
rcsysinit_fname=$3
rcs_fname=$4
usage="USAGE: $0 [-nofat] <topdir> [rcsysinitfile] [<rcsfile>]"
# Verify if we have the optional "-nofat"
if [ "$nofat" == "-nofat" ]; then
echo "We will not mount a FAT/RAMDISK!"
usefat=false
else
topdir=$1
rcsysinit_fname=$2
rcs_fname=$3
fi
if [ -z "$topdir" -o ! -d "$topdir" ]; then
echo "The full path to the NuttX base directory must be provided on the command line"
echo $usage
exit 1
fi
# Verify if we have the optional "rcsysinit_fname" and "rcs_fname"
if [ ! -z "$rcsysinit_fname" ]; then
rcsysinittemplate=$rcsysinit_fname
echo "Target template is $rcsysinittemplate"
fi
if [ ! -z "$rcs_fname" ]; then
rcstemplate=$rcs_fname
echo "Target template is $rcstemplate"
fi
# Extract all values from the .config in the $topdir that contains all of the NuttX
# configuration settings. The .config file was intended to be include-able by makefiles
# and source-able by scripts. Unfortunately,there are too many syntactic difference
# to make that practical
if [ ! -r $topdir/.config ]; then
echo "No readable file at $topdir/.config"
echo "Has NuttX been configured?"
exit 1
fi
romfsetc=`grep CONFIG_NSH_ROMFSETC= $topdir/.config | cut -d'=' -f2`
disablempt=`grep CONFIG_DISABLE_MOUNTPOINT= $topdir/.config | cut -d'=' -f2`
disablescript=`grep CONFIG_NSH_DISABLESCRIPT= $topdir/.config | cut -d'=' -f2`
devconsole=`grep CONFIG_DEV_CONSOLE= $topdir/.config | cut -d'=' -f2`
romfs=`grep CONFIG_FS_ROMFS= $topdir/.config | cut -d'=' -f2`
romfsmpt=`grep CONFIG_NSH_ROMFSMOUNTPT= $topdir/.config | cut -d'=' -f2`
initscript=`grep CONFIG_NSH_INITSCRIPT= $topdir/.config | cut -d'=' -f2`
sysinitscript=`grep CONFIG_NSH_SYSINITSCRIPT= $topdir/.config | cut -d'=' -f2`
romfsdevno=`grep CONFIG_NSH_ROMFSDEVNO= $topdir/.config | cut -d'=' -f2`
romfssectsize=`grep CONFIG_NSH_ROMFSSECTSIZE= $topdir/.config | cut -d'=' -f2`
# If we disabled FAT FS requirement, we don't need to check it
if [ "$usefat" = true ]; then
fatfs=`grep CONFIG_FS_FAT= $topdir/.config | cut -d'=' -f2`
fatdevno=`grep CONFIG_NSH_FATDEVNO= $topdir/.config | cut -d'=' -f2`
fatsectsize=`grep CONFIG_NSH_FATSECTSIZE= $topdir/.config | cut -d'=' -f2`
fatnsectors=`grep CONFIG_NSH_FATNSECTORS= $topdir/.config | cut -d'=' -f2`
fatmpt=`grep CONFIG_NSH_FATMOUNTPT= $topdir/.config | cut -d'=' -f2`
fi
# The following settings are required for general ROMFS support
#
# Mountpoint support must be enabled
if [ "X$disablempt" = "Xy" ]; then
echo "Mountpoint support is required for this feature"
echo "Set CONFIG_DISABLE_MOUNTPOINT=n to continue"
exit 1
fi
# Scripting support must be enabled
if [ "X$disablescript" = "Xy" ]; then
echo "NSH scripting support is required for this feature"
echo "Set CONFIG_NSH_DISABLESCRIPT=n to continue"
exit 1
fi
# ROMFS support is required, of course
if [ "X$romfs" != "Xy" ]; then
echo "ROMFS support is disabled in the NuttX configuration"
echo "Set CONFIG_FS_ROMFS=y to continue"
exit 0
fi
# If it is the default rcS.template, then it also requires FAT FS support
if [ "$usefat" = true -a "X$fatfs" != "Xy" ]; then
echo "FAT FS support is disabled in the NuttX configuration"
echo "Set CONFIG_FS_FAT=y to continue"
exit 0
fi
# Verify that genromfs has been installed
genromfs -h 1>/dev/null 2>&1 || { \
echo "Host executable genromfs not available in PATH"; \
echo "You may need to download in from http://romfs.sourceforge.net/"; \
exit 1; \
}
# Supply defaults for all un-defined ROMFS settings
if [ -z "$romfsmpt" ]; then
romfsmpt=\"/etc\"
fi
if [ -z "$initscript" ]; then
initscript=\"init.d/rcS\"
fi
if [ -z "$sysinitscript" ]; then
sysinitscript=\"init.d/rc.sysinit\"
fi
if [ -z "$romfsdevno" ]; then
romfsdevno=0
fi
if [ -z "$romfssectsize" ]; then
romfssectsize=64
fi
# If FAT FS is a requirement
if [ "$usefat" = true ]; then
# Supply defaults for all un-defined FAT FS settings
if [ -z "$fatdevno" ]; then
fatdevno=1
fi
if [ -z "$fatsectsize" ]; then
fatsectsize=512
fi
if [ -z "$fatnsectors" ]; then
fatnsectors=1024
fi
if [ -z "$fatmpt" ]; then
fatmpt=\"/tmp\"
fi
fi
# Verify the mountpoint. Verify that it is an absolute path but not /, /dev,
# /., /./*, /.., or /../*
if [ ${romfsmpt:0:1} != "\"" ]; then
echo "CONFIG_NSH_ROMFSMOUNTPT must be a string"
echo "Change it so that it is enclosed in quotes."
exit 1
fi
uromfsmpt=`echo $romfsmpt | sed -e "s/\"//g"`
if [ ${uromfsmpt:0:1} != "/" ]; then
echo "CONFIG_NSH_ROMFSMOUNTPT must be an absolute path in the target FS"
echo "Change it so that it begins with the character '/'. Eg. /etc"
exit 1
fi
tmpdir=$uromfsmpt
while [ ${tmpdir:0:1} == "/" ]; do
tmpdir=${tmpdir:1}
done
if [ -z "$tmpdir" -o "X$tmpdir" = "Xdev" -o "X$tmpdir" = "." -o \
${tmpdir:0:2} = "./" -o "X$tmpdir" = ".." -o ${tmpdir:0:3} = "../" ]; then
echo "Invalid CONFIG_NSH_ROMFSMOUNTPT selection."
exit 1
fi
# Verify that the path to the init file is a relative path and not ., ./*, .., or ../*
if [ ${initscript:0:1} != "\"" ]; then
echo "CONFIG_NSH_INITSCRIPT must be a string"
echo "Change it so that it is enclosed in quotes."
exit 1
fi
uinitscript=`echo $initscript | sed -e "s/\"//g"`
if [ ${uinitscript:0:1} == "/" ]; then
echo "CONFIG_NSH_INITSCRIPT must be an relative path in under $romfsmpt"
echo "Change it so that it begins with the character '/'. Eg. init.d/rcS. "
exit 1
fi
if [ "X$uinitscript" = "." -o ${uinitscript:0:2} = "./" -o \
"X$uinitscript" = ".." -o ${uinitscript:0:3} = "../" ]; then
echo "Invalid CONFIG_NSH_INITSCRIPT selection. Must not begin with . or .."
exit 1
fi
if [ ${sysinitscript:0:1} != "\"" ]; then
echo "CONFIG_NSH_SYSINITSCRIPT must be a string"
echo "Change it so that it is enclosed in quotes."
exit 1
fi
usysinitscript=`echo $sysinitscript | sed -e "s/\"//g"`
if [ ${usysinitscript:0:1} == "/" ]; then
echo "CONFIG_NSH_SYSINITSCRIPT must be an relative path in under $romfsmpt"
echo "Change it so that it begins with the character '/'. Eg. init.d/rc.sysinit. "
exit 1
fi
if [ "X$usysinitscript" = "." -o ${usysinitscript:0:2} = "./" -o \
"X$usysinitscript" = ".." -o ${usysinitscript:0:3} = "../" ]; then
echo "Invalid CONFIG_NSH_SYSINITSCRIPT selection. Must not begin with . or .."
exit 1
fi
# Create a working directory
rm -rf $workingdir || { echo "Failed to remove the old $workingdir"; exit 1; }
mkdir -p $workingdir || { echo "Failed to created the new $workingdir"; exit 1; }
# Create the rc.sysinit file from the rc.sysinit.template
if [ ! -r $rcsysinittemplate ]; then
echo "$rcsysinittemplate does not exist"
rmdir $workingdir
exit 1
fi
# If we are using FAT FS with RAMDISK we need to setup it
if [ "$usefat" = true ]; then
cat $rcsysinittemplate | \
sed -e "s,XXXMKRDMINORXXX,$fatdevno,g" | \
sed -e "s,XXMKRDSECTORSIZEXXX,$fatsectsize,g" | \
sed -e "s,XXMKRDBLOCKSXXX,$fatnsectors,g" | \
sed -e "s,XXXRDMOUNTPOINTXXX,$fatmpt,g" >$rcsysinitfile
else
cp $rcsysinittemplate $rcsysinitfile
fi
# Create the rcS file from the rcS.template
if [ ! -r $rcstemplate ]; then
echo "$rcstemplate does not exist"
rmdir $workingdir
exit 1
fi
cp $rcstemplate $rcsfile
# And install it at the specified relative location
# Fix for BSD install without -D option
mkdir -p $workingdir/$uinitscript
rmdir $workingdir/$uinitscript
install -m 0755 $rcsysinitfile $workingdir/$usysinitscript || \
{ echo "Failed to install $rcsysinitfile at $workingdir/$usysinitscript"; rm -f $rcsysinitfile; exit 1; }
rm -f $rcsysinitfile
install -m 0755 $rcsfile $workingdir/$uinitscript || \
{ echo "Failed to install $rcsfile at $workingdir/$uinitscript"; rm -f $rcsfile; exit 1; }
rm -f $rcsfile
# Now we are ready to make the ROMFS image
genromfs -f $romfsimg -d $workingdir -V "NSHInitVol" || { echo "genromfs failed" ; exit 1 ; }
rm -rf $workingdir || { echo "Failed to remove the old $workingdir"; exit 1; }
# And, finally, create the header file
echo '#include <nuttx/compiler.h>' >${headerfile}
xxd -i ${romfsimg} | sed 's/^unsigned char/const unsigned char aligned_data(4)/g' >>${headerfile} || \
{ echo "ERROR: xxd of $< failed" ; rm -f $romfsimg; exit 1 ; }
rm -f $romfsimg