| 1 |
#!/bin/bash -p |
| 2 |
############################################################################### |
| 3 |
# BRLTTY - A background process providing access to the console screen (when in |
| 4 |
# text mode) for a blind person using a refreshable braille display. |
| 5 |
# |
| 6 |
# Copyright (C) 1995-2026 by The BRLTTY Developers. |
| 7 |
# |
| 8 |
# BRLTTY comes with ABSOLUTELY NO WARRANTY. |
| 9 |
# |
| 10 |
# This is free software, placed under the terms of the |
| 11 |
# GNU Lesser General Public License, as published by the Free Software |
| 12 |
# Foundation; either version 2.1 of the License, or (at your option) any |
| 13 |
# later version. Please see the file LICENSE-LGPL for details. |
| 14 |
# |
| 15 |
# Web Page: http://brltty.app/ |
| 16 |
# |
| 17 |
# This software is maintained by Dave Mielke <[email protected]>. |
| 18 |
############################################################################### |
| 19 |
|
| 20 |
programName="${0##*/}" |
| 21 |
programMessage() { |
| 22 |
echo >&2 "${programName}: ${1}" |
| 23 |
} |
| 24 |
programError() { |
| 25 |
[ -n "${1}" ] && programMessage "${1}" |
| 26 |
exit "${2:-3}" |
| 27 |
} |
| 28 |
syntaxError() { |
| 29 |
programError "${1}" 2 |
| 30 |
} |
| 31 |
|
| 32 |
mount_ext2() { |
| 33 |
mount -o loop -t ext2 "${2}" "${1}" |
| 34 |
} |
| 35 |
unmount_ext2() { |
| 36 |
umount "${1}" |
| 37 |
} |
| 38 |
check_ext2() { |
| 39 |
set -- $(stat -c '%a %s' -f -- "${initrdRoot}") |
| 40 |
initrdAvailableBlocks="${1}" |
| 41 |
initrdBlockSize="${2}" |
| 42 |
set -- $(du --block-size="${initrdBlockSize}" -s "${brlttyInstallRoot}") |
| 43 |
brlttyInstallBlocks="${1}" |
| 44 |
set -- $(stat -c '%s' -- "${initrdImage}") |
| 45 |
initrdImageBytes="${1}" |
| 46 |
initrdImageBlocks=$(( (initrdImageBytes + initrdBlockSize - 1) / initrdBlockSize )) |
| 47 |
initrdPadBlocks=$(( brlttyInstallBlocks - initrdAvailableBlocks )) |
| 48 |
(( (initrdPadBlocks += 200) > 0 )) && { |
| 49 |
initrdMounted=false |
| 50 |
"unmount_${initrdType}" "${initrdRoot}" "${initrdImage}" |
| 51 |
|
| 52 |
dd bs="${initrdBlockSize}" count="${initrdPadBlocks}" if="/dev/zero" of="${initrdImage}" seek="${initrdImageBlocks}" |
| 53 |
e2fsck -y -f "${initrdImage}" && resize2fs "${initrdImage}" || { |
| 54 |
newImage="${temporaryDirectory}/new.${initrdType}" |
| 55 |
cp -RpPd -- "${initrdImage}" "${newImage}" |
| 56 |
mke2fs -q -b "${initrdBlockSize}" -F "${newImage}" |
| 57 |
|
| 58 |
newRoot="${temporaryDirectory}/new" |
| 59 |
mkdir "${newRoot}" |
| 60 |
|
| 61 |
"mount_${initrdType}" "${newRoot}" "${newImage}" |
| 62 |
newMounted=true |
| 63 |
|
| 64 |
"mount_${initrdType}" "${initrdRoot}" "${initrdImage}" |
| 65 |
initrdMounted=true |
| 66 |
cp -RpPd -- "${initrdRoot}/." "${newRoot}" |
| 67 |
initrdMounted=false |
| 68 |
"unmount_${initrdType}" "${initrdRoot}" "${initrdImage}" |
| 69 |
|
| 70 |
newMounted=false |
| 71 |
"unmount_${initrdType}" "${newRoot}" "${newImage}" |
| 72 |
|
| 73 |
cp -- "${newImage}" "${initrdImage}" |
| 74 |
} |
| 75 |
|
| 76 |
"mount_${initrdType}" "${initrdRoot}" "${initrdImage}" |
| 77 |
initrdMounted=true |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
mount_cpio() { |
| 82 |
cd "${1}" |
| 83 |
cpio --quiet -i -I "${2}" |
| 84 |
cd "${workingDirectory}" |
| 85 |
} |
| 86 |
unmount_cpio() { |
| 87 |
cd "${1}" |
| 88 |
[ -n "${2}" ] && { |
| 89 |
find . -print | |
| 90 |
sed -e '/^\.$/d' -e 's/^\.\///' | |
| 91 |
cpio --quiet -o -H newc -O "${2}" |
| 92 |
} |
| 93 |
rm -f -r -- $(ls -1 -A) |
| 94 |
cd "${workingDirectory}" |
| 95 |
} |
| 96 |
check_cpio() { |
| 97 |
: |
| 98 |
} |
| 99 |
|
| 100 |
originalImage="boot.iso" |
| 101 |
modifiedImage="brltty.iso" |
| 102 |
brlttyPattern="brltty-*" |
| 103 |
brailleDrivers="all" |
| 104 |
textTable="en-nabcc" |
| 105 |
invokeShell=false |
| 106 |
|
| 107 |
showUsage() { |
| 108 |
cat <<EOF |
| 109 |
Usage: ${programName} [-option ...] |
| 110 |
The following options are supported: |
| 111 |
-i file The original (input) image (default: ${originalImage}). |
| 112 |
-o file The modified (output) image (default: ${modifiedImage}). |
| 113 |
-a file The BRLTTY archive (default: ${brlttyPattern}). |
| 114 |
-b driver,... The braille driver(s) to include (default: ${brailleDrivers}). |
| 115 |
-t file The text table to build in (default: ${textTable}). |
| 116 |
-s Invoke an interactive shell to inspect the modified image. |
| 117 |
-h Display usage information and exit. |
| 118 |
EOF |
| 119 |
exit 0 |
| 120 |
} |
| 121 |
|
| 122 |
while getopts ":i:o:a:b:t:sh" option |
| 123 |
do |
| 124 |
case "${option}" |
| 125 |
in |
| 126 |
i) originalImage="${OPTARG}";; |
| 127 |
o) modifiedImage="${OPTARG}";; |
| 128 |
a) brlttyPattern="${OPTARG}";; |
| 129 |
b) brailleDrivers="${OPTARG}";; |
| 130 |
t) textTable="${OPTARG}";; |
| 131 |
s) invokeShell=true;; |
| 132 |
h) showUsage;; |
| 133 |
\?) syntaxError "unknown option: -${OPTARG}";; |
| 134 |
:) syntaxError "missing operand: -${OPTARG}";; |
| 135 |
*) syntaxError "unimplemented option: -${option}";; |
| 136 |
esac |
| 137 |
done |
| 138 |
shift $((OPTIND - 1)) |
| 139 |
[ "${#}" -gt 0 ] && syntaxError "too many parameters." |
| 140 |
|
| 141 |
[ -f "${originalImage}" ] || programError "original image not found: ${originalImage}" |
| 142 |
|
| 143 |
shopt -s nullglob extglob |
| 144 |
set -- $brlttyPattern |
| 145 |
[ "${#}" -eq 0 ] && programError "brltty archive not found: ${brlttyPattern}" |
| 146 |
[ "${#}" -gt 1 ] && { |
| 147 |
brlttyArchives="$(echo "${*}" | sed -e 's% %,&%g')" |
| 148 |
programError "more than one brltty archive: ${brlttyArchives}" |
| 149 |
} |
| 150 |
brlttyArchive="${1}" |
| 151 |
|
| 152 |
cleanup() { |
| 153 |
set +e |
| 154 |
cd / |
| 155 |
"${newMounted}" && "unmount_${initrdType}" "${newRoot}" |
| 156 |
"${initrdMounted}" && "unmount_${initrdType}" "${initrdRoot}" |
| 157 |
"${originalMounted}" && umount "${originalRoot}" |
| 158 |
[ -n "${temporaryDirectory}" ] && rm -r "${temporaryDirectory}" |
| 159 |
} |
| 160 |
set -e |
| 161 |
originalMounted=false |
| 162 |
initrdMounted=false |
| 163 |
newMounted=false |
| 164 |
trap "cleanup" 0 |
| 165 |
|
| 166 |
workingDirectory="${PWD}" |
| 167 |
temporaryDirectory="$(mktemp -d "${TMPDIR:-/tmp}/${programName}.XXXXXX")" |
| 168 |
|
| 169 |
originalRoot="${temporaryDirectory}/original" |
| 170 |
mkdir "${originalRoot}" |
| 171 |
mount -o loop -t iso9660 "${originalImage}" "${originalRoot}" |
| 172 |
originalMounted=true |
| 173 |
|
| 174 |
modifiedRoot="${temporaryDirectory}/modified" |
| 175 |
mkdir "${modifiedRoot}" |
| 176 |
cp -RpPd -- "${originalRoot}/." "${modifiedRoot}" |
| 177 |
|
| 178 |
loaderDirectory="" |
| 179 |
for directory in boot/isolinux isolinux |
| 180 |
do |
| 181 |
[ -d "${originalRoot}/${directory}" ] && { |
| 182 |
loaderDirectory="${directory}" |
| 183 |
break |
| 184 |
} |
| 185 |
done |
| 186 |
|
| 187 |
configurationFile="isolinux.cfg" |
| 188 |
sed -n -e ' |
| 189 |
1i\ |
| 190 |
prompt 1\ |
| 191 |
timeout 0\ |
| 192 |
default text\ |
| 193 |
|
| 194 |
/^ *prompt /d |
| 195 |
/^ *timeout /d |
| 196 |
/^ *default /d |
| 197 |
/^ *menu *default *$/d |
| 198 |
/^ *append.* text/s/$/ nofb/ |
| 199 |
/^ *append.* rescue/s/$/ text nofb/ |
| 200 |
p |
| 201 |
' <"${originalRoot}/${loaderDirectory}/${configurationFile}" >"${modifiedRoot}/${loaderDirectory}/${configurationFile}" |
| 202 |
|
| 203 |
# bootMessage="boot.msg" |
| 204 |
# echo -n -e '\a' >"${modifiedRoot}/${loaderDirectory}/${bootMessage}" |
| 205 |
# cat "${originalRoot}/${loaderDirectory}/${bootMessage}" >>"${modifiedRoot}/${loaderDirectory}/${bootMessage}" |
| 206 |
|
| 207 |
initrdImage="${temporaryDirectory}/initrd.img" |
| 208 |
initrdPath="${modifiedRoot}/${loaderDirectory}/initrd.img" |
| 209 |
gunzip -c "${initrdPath}" >"${initrdImage}" |
| 210 |
|
| 211 |
initrdDescription="`file -b "${initrdImage}"`" |
| 212 |
case "${initrdDescription}" |
| 213 |
in |
| 214 |
*" ext2 "*) initrdType=ext2;; |
| 215 |
*" cpio "*) initrdType=cpio;; |
| 216 |
*) programError "unsupported initrd image format: ${initrdDescription}" |
| 217 |
esac |
| 218 |
|
| 219 |
initrdRoot="${temporaryDirectory}/initrd" |
| 220 |
mkdir "${initrdRoot}" |
| 221 |
"mount_${initrdType}" "${initrdRoot}" "${initrdImage}" |
| 222 |
initrdMounted=true |
| 223 |
|
| 224 |
found=false |
| 225 |
for name in linuxrc init |
| 226 |
do |
| 227 |
linuxrcPath="${initrdRoot}/${name}" |
| 228 |
initPath="$(readlink "${linuxrcPath}")" && { |
| 229 |
found=true |
| 230 |
break |
| 231 |
} |
| 232 |
done |
| 233 |
"${found}" || programError "linuxrc not found." |
| 234 |
rm "${linuxrcPath}" |
| 235 |
ln -s /bin/brltty "${linuxrcPath}" |
| 236 |
|
| 237 |
deviceDirectory="${initrdRoot}/dev" |
| 238 |
ensureDevices() { |
| 239 |
typeset type="${1}" |
| 240 |
typeset major="${2}" |
| 241 |
typeset minor="${3}" |
| 242 |
typeset prefix="${4}" |
| 243 |
typeset last="${5}" |
| 244 |
typeset suffix="${6}" |
| 245 |
typeset number=0 |
| 246 |
while ((number <= last)) |
| 247 |
do |
| 248 |
typeset path="${deviceDirectory}/${prefix}${suffix}" |
| 249 |
[ -e "${path}" ] || mknod -m 600 "${path}" "${type}" "${major}" "$((minor++))" |
| 250 |
suffix=$((++number)) |
| 251 |
done |
| 252 |
} |
| 253 |
ensureDevices c 7 0 vcs 12 |
| 254 |
ensureDevices c 7 128 vcsa 12 |
| 255 |
ensureDevices c 5 1 console 0 |
| 256 |
ensureDevices c 4 0 tty 12 0 |
| 257 |
ensureDevices c 4 64 ttyS 3 0 |
| 258 |
ensureDevices c 188 0 ttyUSB 1 0 |
| 259 |
|
| 260 |
case "${brlttyArchive}" |
| 261 |
in |
| 262 |
*.tar.gz|*.tgz) |
| 263 |
tar x -z -C "${temporaryDirectory}" -f "${brlttyArchive}" |
| 264 |
;; |
| 265 |
|
| 266 |
*.tar) |
| 267 |
tar x -C "${temporaryDirectory}" -f "${brlttyArchive}" |
| 268 |
;; |
| 269 |
|
| 270 |
*) |
| 271 |
programError "unsupported brltty archive name: ${brlttyArchive}" |
| 272 |
;; |
| 273 |
esac |
| 274 |
|
| 275 |
shopt -s nullglob |
| 276 |
set -- ${temporaryDirectory}/brltty* |
| 277 |
[ "${#}" -eq 0 ] && programError "brltty build directory not found." |
| 278 |
brlttyBuildRoot="${1}" |
| 279 |
|
| 280 |
brlttyInstallRoot="${temporaryDirectory}/install" |
| 281 |
( |
| 282 |
set -e |
| 283 |
trap "" 0 |
| 284 |
cd "${brlttyBuildRoot}" |
| 285 |
|
| 286 |
shopt -s nullglob |
| 287 |
set -- ./autogen* |
| 288 |
for autogen |
| 289 |
do |
| 290 |
[ -f "${autogen}" -a -r "${autogen}" -a -x "${autogen}" ] && "${autogen}" |
| 291 |
done |
| 292 |
|
| 293 |
./configure --quiet --with-install-root="${brlttyInstallRoot}" \ |
| 294 |
--with-init-path="${initPath}" --with-stderr-path="/dev/tty5" \ |
| 295 |
--with-writable-directory="/tmp" \ |
| 296 |
--with-screen-driver="lx,-all" \ |
| 297 |
--with-braille-driver="-tt,-vr,${brailleDrivers}" \ |
| 298 |
--with-text-table="${textTable}" \ |
| 299 |
--disable-speech-support --without-libbraille --without-curses --disable-x \ |
| 300 |
--disable-pcm-support --disable-fm-support --disable-midi-support \ |
| 301 |
--enable-standalone-programs --disable-api --disable-gpm --disable-icu |
| 302 |
make -s -C Programs all |
| 303 |
strip Programs/brltty |
| 304 |
make -s -C Programs install-programs install-data-files install-tables |
| 305 |
) || exit "${?}" |
| 306 |
|
| 307 |
"check_${initrdType}" |
| 308 |
( |
| 309 |
set -e |
| 310 |
cd "${brlttyInstallRoot}" |
| 311 |
cp --parents -- $(find . -type f -printf '%P ') "${initrdRoot}" |
| 312 |
) || exit 1 |
| 313 |
|
| 314 |
"${invokeShell}" && ( |
| 315 |
set +e |
| 316 |
trap "" 0 |
| 317 |
|
| 318 |
cd "${temporaryDirectory}" |
| 319 |
"${SHELL:-/bin/sh}" |
| 320 |
exit 0 |
| 321 |
) |
| 322 |
|
| 323 |
initrdMounted=false |
| 324 |
"unmount_${initrdType}" "${initrdRoot}" "${initrdImage}" |
| 325 |
gzip -c -9 "${initrdImage}" >"${initrdPath}" |
| 326 |
|
| 327 |
isoVolumeIdentifier="$(isoinfo -d -i "${originalImage}" | grep '^Volume id:' | cut -d' ' -f3-)" |
| 328 |
isoApplicationIdentifier="$(isoinfo -d -i "${originalImage}" | grep '^Application id:' | cut -d' ' -f3-)" |
| 329 |
mkisofs -quiet -J -R -T -boot-load-size 4 -boot-info-table -no-emul-boot \ |
| 330 |
-A "${isoApplicationIdentifier}" -V "${isoVolumeIdentifier}" \ |
| 331 |
-b "${loaderDirectory}/isolinux.bin" -c "${loaderDirectory}/boot.cat" \ |
| 332 |
-o "${modifiedImage}" "${modifiedRoot}" |
| 333 |
exit 0 |