Debian Codegraph @

functions

raw
1 # Copyright (C) 2006 Joey Hess
2 # Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2016 Martin Michlmayr <[email protected]>
3 # Copyright (C) 2011 Loïc Minier <[email protected]>
4 # Copyright (C) 2011 Julian Andres Klode <[email protected]>
5 # Copyright (C) 2013-2016 Ian Campbell <[email protected]>
6
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
20 # USA.
21
22 BOOTSCRIPTS_DIR="${FK_CHECKOUT:-/etc/flash-kernel}/bootscript"
23 FK_ETC_MACHINE="${FK_ETC_MACHINE:-/etc/flash-kernel/machine}"
24 PROC_CPUINFO="${FK_PROC_CPUINFO:-/proc/cpuinfo}"
25 PROC_DTMODEL="${FK_PROC_DTMODEL:-/proc/device-tree/model}"
26 PROC_MTD="/proc/mtd"
27
28 read_machine_db() {
29 if [ -f "${FK_ETC_DB:-/etc/flash-kernel/db}" ]; then
30 cat "${FK_ETC_DB:-/etc/flash-kernel/db}"
31 fi
32 cat "${FK_CHECKOUT:-$FK_DIR}/db/"*.db
33 }
34 MACHINE_DB="$(read_machine_db)"
35
36 error() {
37 echo "$@" >&2
38 exit 1
39 }
40
41 mtdblock() {
42 local mtdname="$1"
43
44 local dev=`sed -rn "s,^mtd([^:]*).*\"$mtdname\"\$,/dev/mtdblock\\1,p" "$PROC_MTD"`
45
46 # The mtdblock() function gets also called by the testsuite during
47 # the package build; don't run modprobe and udevadm then. Invasive
48 # actions like loading modules and calling into udev should not
49 # happen at build time and they are not necessary for the testsuite.
50 if [ -z "${FK_TESTSUITE_RUNNING}" ]; then
51 modprobe -q mtdblock && udevadm settle --exit-if-exists=$dev || :
52 fi
53
54 echo $dev
55 }
56
57 mtdchar() {
58 local mtdname="$1"
59
60 local dev=`sed -rn "s,^mtd([^:]*).*\"$mtdname\"\$,/dev/mtd\\1,p" "$PROC_MTD"`
61
62 echo $dev
63 }
64
65 check_block_dev() {
66 local dev="$1"
67
68 if [ ! -b "$dev" ]; then
69 error "$dev is not a block device"
70 fi
71 }
72
73 check_char_dev() {
74 local dev="$1"
75
76 if [ ! -c "$dev" ]; then
77 error "$dev is not a character device"
78 fi
79 }
80
81 mtdsize() {
82 local mtdname="$1"
83
84 size=$(grep "\"$mtdname\"$" "$PROC_MTD" | cut -d " " -f 2)
85 printf "%d" "0x$size"
86 }
87
88 check_kflavors() {
89 local kvers="$1"
90 local kflavor="$2"
91 if [ "$kflavor" = "any" ]; then
92 return 0
93 fi
94 # count flavor+ as valid
95 kvers=${kvers%%+}
96 if [ "${kvers}" != "${kvers%%$kflavor}" ]; then
97 # kernel version ended with flavor
98 return 0
99 fi
100 return 1
101 }
102
103 check_mtd_size() {
104 local mtd_name="$1"
105 local required_size="$2"
106 local actual_size="$3"
107 local what="$4"
108
109 if [ $required_size -gt $actual_size ]; then
110 case $what in
111 initrd)
112 (
113 echo
114 echo "The initial ramdisk is too large. This is often due to the unnecessary inclusion"
115 echo "of all kernel modules in the image. To fix this set MODULES=dep in one or both"
116 echo "/etc/initramfs-tools/conf.d/driver-policy (if it exists) and"
117 echo "/etc/initramfs-tools/initramfs.conf and then run 'update-initramfs -u -k $kvers'"
118 echo
119 ) >&2
120 ;;
121 esac
122 error "Not enough space for $what in MTD '$mtd_name' (need $required_size but is actually $actual_size)."
123 fi
124 }
125
126 check_supported() {
127 local machine="$1"
128 local field
129 local value
130
131 echo "$MACHINE_DB" | {
132 while read field value; do
133 if [ "$field" = "Machine:" ] &&
134 [ "$value" = "$machine" ]; then
135 return 0
136 fi
137 done
138 return 1
139 }
140 }
141
142 get_boot_cmd() {
143 case "$(dpkg --print-architecture)" in
144 arm64|riscv64) echo booti ;;
145 *) echo bootz ;;
146 esac
147 }
148
149 get_cpuinfo_hardware() {
150 grep "^Hardware" "$PROC_CPUINFO" | sed 's/Hardware\s*:\s*//'
151 }
152 get_dt_model() {
153 cat "$PROC_DTMODEL"
154 }
155 get_machine() {
156 if [ -n "$FK_MACHINE" ]; then
157 machine="$FK_MACHINE"
158 elif [ -f "$FK_ETC_MACHINE" ]; then
159 machine="$(cat "$FK_ETC_MACHINE")"
160 elif [ -f "$PROC_DTMODEL" ]; then
161 machine="$(get_dt_model)"
162 else
163 machine="$(get_cpuinfo_hardware)"
164 fi
165 }
166
167 get_kfile_suffix() {
168 local kfile="$1"
169 local tail="${2:+-$2}"
170
171 echo "$kfile" | sed -e "s/.*-\([^-]*$tail\)/\\1/"
172 }
173
174 # this is case-sensitive and doesn't support fields spanning multiple lines
175 get_machine_field() {
176 local machine="$1"
177 local field_name="$2"
178 local state="machine"
179 local field
180 local value
181
182 # State machine is:
183 # machine: Looking for "Machine:"
184 # gotmachine: Seen matching "Machine:", possibly consumning other
185 # alternative (non-matching) "Machine:"s waiting for
186 # non-"Machine:" field.
187 # fields: seen non-"Machine:" field.
188
189 echo "$MACHINE_DB" | {
190 while read field value; do
191 if [ "$state" = "machine" ] &&
192 [ "$field" = "Machine:" ] &&
193 [ "$value" = "$machine" ]; then
194 state="gotmachine"
195 fi
196 if [ "$state" = "fields" ] || [ "$state" = "gotmachine" ]; then
197 case "$field" in
198 "${field_name}:")
199 echo "$value"
200 return 0
201 ;;
202 Machine:)
203 if [ "$state" != "gotmachine" ]; then
204 echo "DB syntax invalid, got $field $value, expected regular Field" >&2
205 return 1
206 fi
207 ;;
208 "")
209 state="machine"
210 ;;
211 *)
212 state="fields"
213 ;;
214 esac
215 fi
216 done
217 return 1
218 }
219 }
220
221 get_mkimage_architecture() {
222 kfile_suffix=$(get_kfile_suffix "$1" "")
223 case "$kfile_suffix" in
224 "arm64") echo "arm64";;
225 *) echo "arm";;
226 esac
227 }
228
229 get_dtb_name() {
230 local field="$(get_machine_field "$machine" "DTB-Id")" || :
231 case "$field" in
232 !*)
233 local dir
234 local dtb_script
235 dtb_script_name=${field#!}
236 for dir in /etc/flash-kernel/dtb-probe \
237 /usr/share/flash-kernel/dtb-probe ; do
238 if [ -e "$dir/$dtb_script_name" ]; then
239 dtb_script="$dir/$dtb_script_name"
240 break
241 fi
242 done
243
244 if [ "x$dtb_script" = "x" ]; then
245 error "dtb-probe $dtb_script_name not found"
246 fi
247
248 dtb_name=$($dtb_script $kvers)
249 if [ $? -ne 0 ] || [ "x$dtb_name" = "x" ]; then
250 error "dtb-probe $dtb_script failed"
251 fi
252 ;;
253 *)
254 dtb_name="$field"
255 ;;
256 esac
257 if [ -n "$dtb_name" ] ; then
258 echo "Using DTB: $dtb_name" >&2
259 fi
260 dtb_dir=$(dirname "$dtb_name")
261 dtb_name=$(basename "$dtb_name")
262 }
263
264 machine_uses_flash() {
265 local machine="$1"
266
267 if ! check_supported "$machine"; then
268 # assume devices not explicitly listed are using flash
269 return 0
270 fi
271
272 if [ -n "$(get_machine_field "$machine" "Mtd-Kernel")" ] ||
273 [ -n "$(get_machine_field "$machine" "Mtd-Initrd")" ]; then
274 return 0
275 fi
276 return 1
277 }
278
279 # output ARM instructions to set machine number; argument is the decimal
280 # machine number as found in linux/arch/arm/tools/mach-types
281 set_machine_id() {
282 local machine_id="$1"
283 local high
284 local low
285
286 if [ -z "$machine_id" ]; then
287 return
288 fi
289
290 high="$(printf "%02x" $(($machine_id / 256)))"
291 low="$(printf "%02x" $(($machine_id % 256)))"
292
293 devio "wl 0xe3a01c$high,4" "wl 0xe38110$low,4"
294 }
295
296 gen_kernel() {
297 local input="$1"
298 local output="$2"
299 local machine_id="$3"
300
301 {
302 set_machine_id "$machine_id"
303 cat "$input"
304 } >"$output"
305 }
306
307 gen_preboot() {
308 PRESTUBDIRS="/etc/flash-kernel/preboot.d /usr/share/flash-kernel/preboot.d"
309 PRESTUBS="$(find $PRESTUBDIRS -type f -regex '.*/[0-9a-zA-Z_-]+' -printf '%f\n' | LC_ALL=C sort -u)"
310 for file in $PRESTUBS; do
311 for dir in $PRESTUBDIRS; do
312 if [ -f $dir/$file ]; then
313 cat $dir/$file
314 break
315 fi
316 done
317 done
318 }
319
320 gen_ubootenv() {
321 ENVSTUBDIRS="/etc/flash-kernel/ubootenv.d /usr/share/flash-kernel/ubootenv.d"
322 ENVSTUBS="$(find $ENVSTUBDIRS -type f -regex '.*/[0-9a-zA-Z_-]+' -printf '%f\n' | LC_ALL=C sort -u)"
323 for file in $ENVSTUBS; do
324 for dir in $ENVSTUBDIRS; do
325 if [ -f $dir/$file ]; then
326 cat $dir/$file
327 break
328 fi
329 done
330 done
331 }
332
333 append_dtb() {
334 local kernel="$1"
335 local dtb="$2"
336 local output="$3"
337
338 echo "flash-kernel: appending $dtb to kernel" >&2
339
340 {
341 cat "$kernel"
342 cat "$dtb"
343 } >"$output"
344 }
345
346 write_mtd() {
347 local input_file="$1"
348 local output_mtd="$2"
349 local base_mtd=$(basename $output_mtd)
350 local mtd_backup_dir=$(get_mtd_backup_dir)
351 local tmpfile
352
353 if [ "x$input_file" = "x-" ] ; then
354 tmpfile=$(mktemp -t "$self.$base_mtd.XXXXXX") || error "Failed"
355 cat > $tmpfile
356 input_file="$tmpfile"
357 fi
358
359 # Keep a copy in a convenient place for backups etc
360 if [ -n "${mtd_backup_dir}" ] ; then
361 local backup="${mtd_backup_dir}/${base_mtd}"
362 #echo "Saving new $output_mtd in ${backup}."
363 mkdir -p "${mtd_backup_dir}"
364 cp "$input_file" "${backup}"
365 fi
366
367 # Can't really flashcp to /dev/mtd<N> when testing
368 if [ -z "${FK_TESTSUITE_RUNNING}" ]; then
369 local flashtype=$(cat /sys/class/mtd/$base_mtd/type)
370 case "$flashtype" in
371 nand|mlc-nand)
372 flash_erase "$output_mtd" 0 0
373 nandwrite -p "$output_mtd" "$input_file"
374 ;;
375 nor)
376 flashcp "$input_file" "$output_mtd"
377 ;;
378 *)
379 error "unsupported flash type"
380 ;;
381 esac
382 else
383 cp "$input_file" "$output_mtd"
384 fi
385
386 if [ "$tmpfile" ] ; then
387 rm -f "$tmpfile"
388 fi
389 }
390
391 flash_kernel() {
392 local input_file="$1"
393 local output_mtd="$2"
394 local machine_id="$3"
395 local use
396
397 if [ -n "$kmtdsize" ]; then
398 kreqsize=$(stat -c '%s' "$input_file")
399 if [ -n "$machine_id" ]; then
400 kreqsize=$(($kreqsize + 8))
401 fi
402 check_mtd_size "$mtd_kernel" $kreqsize $kmtdsize kernel
403 use=" (using $kreqsize/$kmtdsize bytes)"
404 fi
405
406 printf "Flashing kernel$use... " >&2
407 gen_kernel "$input_file" "$tmpdir/flash_kernel.raw" "$machine_id" || error "failed."
408 write_mtd "$tmpdir/flash_kernel.raw" "$output_mtd" || error "failed."
409 echo "done." >&2
410 }
411
412 flash_initrd() {
413 local input_file="$1"
414 local output_mtd="$2"
415 local pad="$3"
416 local use
417
418 if [ -n "$imtdsize" ]; then
419 use=" (using $ireqsize/$imtdsize bytes)"
420 fi
421
422 printf "Flashing initramfs$use... " >&2
423 {
424 cat "$input_file"
425 if [ "$pad" -gt 0 ]; then
426 dd if=/dev/zero bs="$pad" count=1 2>/dev/null
427 fi
428 } | write_mtd "-" "$output_mtd" || error "failed."
429 echo "done." >&2
430 }
431
432 get_kernel_cmdline() {
433 . /etc/default/flash-kernel
434 echo "$LINUX_KERNEL_CMDLINE"
435 }
436 get_kernel_cmdline_defaults() {
437 . /etc/default/flash-kernel
438 echo "$LINUX_KERNEL_CMDLINE_DEFAULTS"
439 }
440
441 mkimage_kernel() {
442 local kaddr="$1"
443 local epoint="$2"
444 local kdesc="$3"
445 local kdata="$4"
446 local uimage="$5"
447
448 printf "Generating kernel u-boot image... " >&2
449 mkimage -A "$mkarch" -O linux -T kernel -C none -a "$kaddr" -e "$epoint" \
450 -n "$kdesc" -d "$kdata" "$uimage" >&2 1>/dev/null
451 echo "done." >&2
452 }
453
454 mkimage_initrd() {
455 local iaddr="$1"
456 local idesc="$2"
457 local idata="$3"
458 local uinitrd="$4"
459
460 printf "Generating initramfs u-boot image... " >&2
461 mkimage -A "$mkarch" -O linux -T ramdisk -C none -a "$iaddr" -e "$iaddr" \
462 -n "$idesc" -d "$idata" "$uinitrd" >&2 1>/dev/null
463 echo "done." >&2
464 }
465
466 mkimage_script() {
467 local saddr="$1"
468 local sdesc="$2"
469 local sdata="$3"
470 local script="$4"
471
472 local tdata="$tmpdir/$(basename $sdata).out"
473
474 local ubootenv="$(mktemp --tmpdir=$tmpdir)"
475 gen_ubootenv > $ubootenv
476 local preboot="$(mktemp --tmpdir=$tmpdir)"
477 gen_preboot > $preboot
478
479 if [ "$(stat --printf='%s' $ubootenv)" -gt 0 ] && \
480 ! grep -q '@@UBOOT_ENV_EXTRA@@' "$sdata" ; then
481 echo "WARNING: ubootenv.d snippet used, but $sdata has no @@UBOOT_ENV_EXTRA@@ marker. Snippet will be ignored." >&2
482 fi
483
484 printf "Generating boot script u-boot image... " >&2
485 sed -e "s/@@KERNEL_VERSION@@/$kvers/g" \
486 -e "s!@@LINUX_KERNEL_CMDLINE@@!$(get_kernel_cmdline)!g" \
487 -e "s!@@LINUX_KERNEL_CMDLINE_DEFAULTS@@!$(get_kernel_cmdline_defaults)!g" \
488 -e "s!@@BOOT_CMD@@!$(get_boot_cmd)!g" \
489 -e "/@@UBOOT_ENV_EXTRA@@/{
490 s/@@UBOOT_ENV_EXTRA@@//g
491 r $ubootenv
492 }" < $sdata > $tdata \
493 -e "/@@UBOOT_PREBOOT_EXTRA@@/{
494 s/@@UBOOT_PREBOOT_EXTRA@@//g
495 r $preboot
496 }" < $sdata > $tdata
497 mkimage -A "$mkarch" -O linux -T script -C none -a "$saddr" -e "$saddr" \
498 -n "$sdesc" -d "$tdata" "$script" >&2 1>/dev/null
499 echo "done." >&2
500 }
501
502 mkimage_multi() {
503 local maddr="$1"
504 local mdesc="$2"
505 local kdata="$3"
506 local idata="$4"
507 local umulti="$5"
508 local images="$kdata"
509
510 [ -z "$idata" ] || images="$images:$idata"
511
512 printf "Generating u-boot image..." >&2
513 mkimage -A "$mkarch" -O linux -T multi -C none -a "$maddr" -e "$maddr" \
514 -n "$mdesc" -d "$images" "$umulti" >&2 1>/dev/null
515 echo "done." >&2
516 }
517
518 # Return a nonempty string *unless* NO_CREATE_DOT_BAK_FILES is set.
519 get_dot_bak_preference() {
520 . /etc/default/flash-kernel
521 case $(echo "$NO_CREATE_DOT_BAK_FILES" | tr '[:upper:]' '[:lower:]') in
522 true|yes|1) ;;
523 *) echo yes ;;
524 esac
525 }
526
527 # Return a nonempty string *unless* MTD_BACKUP_DIR is "none".
528 get_mtd_backup_dir() {
529 . /etc/default/flash-kernel
530 case "${MTD_BACKUP_DIR:=/var/backups/flash-kernel}" in
531 none) echo "";;
532 *) echo "${MTD_BACKUP_DIR}";;
533 esac
534 }
535
536 backup_and_install() {
537 local source="$1"
538 local dest="$2"
539 local do_dot_bak=$(get_dot_bak_preference)
540 local mtd_backup_dir=$(get_mtd_backup_dir)
541 if [ -e "$dest" ]; then
542 if [ -n "$do_dot_bak" ]; then
543 echo "Taking backup of $(basename "$dest")." >&2
544 mv "$dest" "$dest.bak"
545 else
546 echo "Skipping backup of $(basename "$dest")." >&2
547 fi
548 fi
549 # If we are installing to a filesystem which is not normally mounted
550 # then take a second copy in /var/backups, where they can e.g. be
551 # backed up.
552 if [ -n "$boot_mnt_dir" ] && [ -n "$mtd_backup_dir" ] ; then
553 local bak="$mtd_backup_dir/"$(basename "$dest")
554 #echo "Saving $boot_device:"$(basename "$source")" in $bak"
555 mkdir -p "$mtd_backup_dir"
556 cp "$source" "$bak"
557 fi
558 echo "Installing new $(basename "$dest")." >&2
559 mv "$source" "$dest"
560 }
561
562 # See http://www.nslu2-linux.org/wiki/Info/BootFlash -- the NSLU2 uses a
563 # 16 byte MTD header, the first four bytes (big endian) give the length of
564 # the remainder of the image, and the remaining bytes are zero. Generate
565 # this header.
566 sercomm_header() {
567 perl -e 'print pack("N4", shift)' "$1"
568 }
569
570 nslu2_swap() {
571 if [ "$little_endian" -eq 1 ]; then
572 devio "<<$1" "xp $,4"
573 else
574 cat "$1"
575 fi
576 }
577
578 # XXX needs testsuite coverage
579 abootimg_get_image_size() {
580 local abootimg="$1"
581
582 echo "$abootimg" | sed -rn 's/^\* image size = ([0-9]+) bytes.*/\1/p'
583 }
584
585 dtb_append_required() {
586 linux-version compare "$kvers" ge "$dtb_append_from"
587 }
588
589 # XXX needs testsuite coverage
590 android_flash() {
591 local device="$1"
592
593 printf "Flashing kernel and initramfs to $device... " >&2
594 abootimg -u "$device" -k "$kfile" -r "$ifile" >/dev/null ||
595 error "failed."
596 echo "done." >&2
597 }
598
599 get_kernel_status() {
600 # get_kernel_status(query_ver)
601 # return the dpkg package status of kernel version $query_ver
602 local query_ver="$1"
603 local ver path pkg
604 linux-version list --paths | while read -r ver path; do
605 if linux-version compare "$ver" eq "$query_ver"; then
606 pkg=$(
607 LANG=C.UTF-8 dpkg-query --search "$path" 2>/dev/null |
608 awk -F ": " -v path="$path" '$2==path { print $1; }'
609 )
610 LANG=C.UTF-8 dpkg-query --show --showformat '${db:Status-Status}' "$pkg"
611 break
612 fi
613 done
614 }
615
616 find_dtb_file() {
617 local dtb
618 case "$dtb_dir" in
619 /*)
620 dtb="$dtb_dir/$dtb_name"
621 if [ ! -f "$dtb" ]; then
622 error "Couldn't find $dtb"
623 fi
624 ;;
625 *)
626 dtb=$(find /etc/flash-kernel/dtbs -name $dtb_name 2>/dev/null | head -n 1)
627 if [ -z "$dtb" ]; then
628 dtb=$(find -H /usr/lib/linux-image-$kvers -name $dtb_name 2>/dev/null | head -n 1)
629 fi
630 if [ ! -f "$dtb" ]; then
631 error "Couldn't find DTB $dtb_name in /usr/lib/linux-image-$kvers or /etc/flash-kernel/dtbs"
632 fi
633 ;;
634 esac
635 echo $dtb
636 }
637
638 handle_dtb() {
639 if [ "x$dtb_name" = "x" ]; then
640 return
641 fi
642
643 dtbfile="/boot/dtbs/$kvers/$dtb_dir/$dtb_name"
644
645 local dtb
646 if [ "x$FK_KERNEL_HOOK_SCRIPT" = "xpostrm.d" ] ; then
647 rm -f "$dtbfile"
648
649 # This was the old name we installed under. We
650 # currently include it as an alternative symlink.
651 rm -f "/boot/dtb-$kvers"
652
653 if [ -L /boot/dtb ] ; then
654 # Remove if it points to the current kernel
655 # version, both old (dtb-$kvers) and new
656 # (dtbs/...) names
657 case $(readlink /boot/dtb) in
658 "dtb-$kvers") rm -f /boot/dtb ;;
659 "dtbs/$kvers/$dtb_name") rm -f /boot/dtb ;;
660 esac
661 fi
662
663 if [ -d /boot/dtbs/$kvers ] ; then
664 rmdir --ignore-fail-on-non-empty /boot/dtbs/$kvers
665 fi
666 if [ -d /boot/dtbs ] ; then
667 rmdir --ignore-fail-on-non-empty /boot/dtbs
668 fi
669 else
670 local dtb=$(find_dtb_file)
671 echo "Installing $dtb into $dtbfile" >&2
672 mkdir -p "/boot/dtbs/$kvers/$dtb_dir"
673 cp "$dtb" "$dtbfile.new"
674 backup_and_install "$dtbfile.new" "$dtbfile"
675
676 if [ "$dtb_dir" != "." ]; then
677 ln -nfs "$dtb_dir/$dtb_name" "/boot/dtbs/$kvers/$dtb_name"
678 fi
679
680 # Historically we installed the dtb as
681 # dtb-$kvers, keep it around as an alternative
682 # for now. Useful for platforms which do not
683 # set ${fdtfile}
684 ln -nfs "dtbs/$kvers/$dtb_dir/$dtb_name" "/boot/dtb-$kvers"
685
686 # This can be used along with the unversioned
687 # vmlinuz+initrd.gz e.g. as a fallback option
688 ln -nfs "dtbs/$kvers/$dtb_dir/$dtb_name" "/boot/dtb"
689 fi
690 }
691
692 main() {
693 force="no"
694 if [ "x$1" = "x--force" ]; then
695 force="yes"
696 shift
697 fi
698 if [ "x$1" = "x--machine" ]; then
699 machine="$2"
700 shift 2
701 else
702 get_machine
703 fi
704 [ "x$machine" = "xnone" ] && exit 0
705
706 if [ "x$1" = "x--supported" ]; then
707 if check_supported "$machine"; then
708 exit 0
709 fi
710 exit 1
711 fi
712
713 # $FK_KERNEL_HOOK_SCRIPT is set when main() is called from
714 # /etc/kernel/* to be able to differentiate between being called
715 # upon kernel installation or kernel removal
716
717 # kernel + initrd installation/upgrade mode, with optional version
718
719 kvers="$1"
720
721 get_dtb_name
722
723 # Install/remove any DTB from postinst, regardless of version
724 if [ -n "$kvers" ] ; then
725 handle_dtb
726 fi
727
728 latest_version=$(linux-version list | linux-version sort | tail -1)
729
730 if [ -n "$kvers" ] && [ "$FK_KERNEL_HOOK_SCRIPT" = "postrm.d" ]; then
731 echo "flash-kernel: Kernel ${kvers} has been removed." >&2
732 if $(linux-version compare "$kvers" lt "$latest_version"); then
733 echo "flash-kernel: A higher version (${latest_version}) is still installed, no reflashing required." >&2
734 exit 0
735 else
736 if [ -n "${latest_version}" ]; then
737 echo "flash-kernel: Flashing the remaining highest-versioned kernel (${latest_version})." >&2
738 else
739 echo "flash-kernel: WARNING: No other kernel packages found!" >&2
740 echo "flash-kernel: The system might be unbootable." >&2
741 echo "flash-kernel: Please install a kernel package before rebooting the system." >&2
742 exit 0
743 fi
744 fi
745 fi
746
747 # accumulate multiple calls in a trigger to only run flash-kernel once; the
748 # trigger will just call flash-kernel again with FLASH_KERNEL_NOTRIGGER set to
749 # force a real run
750 if [ -z "$FLASH_KERNEL_NOTRIGGER" ] && [ -n "$DPKG_MAINTSCRIPT_PACKAGE" ] && dpkg-trigger --check-supported 2>/dev/null; then
751 # flash-kernel trigger isn't disabled, and we're called from
752 # some package maintainer script (e.g. some postinst calls
753 # flash-kernel), and dpkg-trigger is installed and confirms
754 # that the running dpkg support triggers: we can use the
755 # flash-kernel trigger (asynchronously)
756 if dpkg-trigger --no-await flash-kernel; then
757 echo "flash-kernel: deferring update (trigger activated)" >&2
758 exit 0
759 fi
760 # dpkg-trigger failed for some reason, proceed to a normal run
761 fi
762
763 case "$kvers" in
764 "")
765 if [ "$force" = "yes" ]; then
766 echo "flash-kernel: no kernel version specified with --force." >&2
767 exit 1
768 else
769 kvers="$latest_version"
770 # Make sure we install the DTB for $latest_version
771 handle_dtb
772 fi
773 ;;
774 "$latest_version")
775 # Proceed as normal; --force is basically a noop here
776 ;;
777 *)
778 if [ "$force" = "yes" ]; then
779 echo "flash-kernel: forcing install of ${kvers} instead of ${latest_version}." >&2
780 echo "flash-kernel: WARNING: Installing any new kernel package might override this." >&2
781 else
782 echo "Ignoring old or unknown version $kvers (latest is $latest_version)" >&2
783 exit 0
784 fi
785 ;;
786 esac
787
788 kstatus=$(get_kernel_status "$kvers")
789 if [ "$kstatus" != "installed" ]; then
790 echo "flash-kernel: requested kernel ${kvers} is in state ${kstatus}." >&2
791 if [ -n "$DPKG_RUNNING_VERSION" ]; then
792 # We're running under dpkg; re-trigger ourselves under the assumption
793 # that the status will resolve
794 if dpkg-trigger --no-await flash-kernel; then
795 echo "flash-kernel: deferring update (trigger activated)" >&2
796 exit 0
797 else
798 echo "flash-kernel: deferral failed; proceeding with install" >&2
799 fi
800 elif [ "$force" != "yes" ]; then
801 exit 1
802 fi
803 echo "flash-kernel: WARNING: Components like the initrd may be missing or require regeneration." >&2
804 fi
805
806 kfile="/boot/vmlinuz-$kvers"
807 ifile="/boot/initrd.img-$kvers"
808 desc="kernel $kvers"
809 idesc="ramdisk $kvers"
810
811 if [ ! -e $kfile ]; then
812 kfile="/boot/vmlinux-$kvers"
813 fi
814
815 if [ ! -e "$ifile" ]; then
816 ifile=
817 fi
818
819 if [ ! -e $kfile ]; then
820 # installation-report #781742 included:
821 # Can't find /boot/vmlinuz-[...] or /boot/initrd.img-[...]
822 #
823 # It's unclear how this can have happened or what state the
824 # system was in, so log some additional information in the
825 # hopes of catching it in the act next time.
826 (
827 set -x
828 ls -l $kfile*
829 ls -l $ifile*
830 ) >> /tmp/flash-kernel-no-kernel-error.log 2>&1 || :
831 error "Can't find $kfile (see /tmp/flash-kernel-no-kernel-error.log)"
832 fi
833 kfilesize=$(stat -c '%s' "$kfile")
834 [ -z "$ifile" ] || ifilesize=$(stat -c '%s' "$ifile")
835
836 if [ -L "$kfile" ]; then
837 kfile=$(readlink -e "$kfile")
838 fi
839
840 if [ -f /etc/flash-kernel/ignore-efi ] && [ -z "$FK_IGNORE_EFI" ]; then
841 FK_IGNORE_EFI="$(cat /etc/flash-kernel/ignore-efi)"
842 fi
843 if [ -d /sys/firmware/efi ] && [ "x$FK_IGNORE_EFI" != "xyes" ]; then
844 # skipping when detect EFI
845 echo "System running in EFI mode, skipping."
846 exit 0
847 fi
848
849 if ! check_supported "$machine"; then
850 error "Unsupported platform '$machine'."
851 fi
852
853 kfile_suffix=""
854 if ! kflavors=$(get_machine_field "$machine" "Kernel-Flavors") ; then
855 # Since no Kernel-Flavors were specified, allow any kernel.
856 kflavors="any"
857 fi
858 for kflavor in $kflavors ; do
859 if check_kflavors "$kvers" "$kflavor" ; then
860 kfile_suffix="$kflavor"
861 break
862 fi
863 done
864
865 if [ -z "$kfile_suffix" ]; then
866 echo "Kernel $kfile does not match any of the expected flavors ($kflavors), therefore not writing it to flash." >&2
867 exit 0
868 fi
869
870 echo "flash-kernel: installing version $kvers" >&2
871
872 mkarch="$(get_mkimage_architecture $kvers)"
873 machine_id="$(get_machine_field "$machine" "Machine-Id")" || :
874 method="$(get_machine_field "$machine" "Method")" || method="generic"
875 mtd_kernel="$(get_machine_field "$machine" "Mtd-Kernel")" || :
876 mtd_initrd="$(get_machine_field "$machine" "Mtd-Initrd")" || :
877 dtb_append="$(get_machine_field "$machine" "DTB-Append")" || :
878 dtb_append_from="$(get_machine_field "$machine" "DTB-Append-From")" || :
879 ukaddr="$(get_machine_field "$machine" "U-Boot-Kernel-Address")" || :
880 ukepoint="$(get_machine_field "$machine" "U-Boot-Kernel-Entry-Point")" || :
881 uiaddr="$(get_machine_field "$machine" "U-Boot-Initrd-Address")" || :
882 umaddr="$(get_machine_field "$machine" "U-Boot-Multi-Address")" || :
883 usaddr="$(get_machine_field "$machine" "U-Boot-Script-Address")" || :
884 usname="$(get_machine_field "$machine" "U-Boot-Script-Name")" || :
885 boot_device="$(get_machine_field "$machine" "Boot-Device")" || :
886 boot_kernel_path="$(get_machine_field "$machine" "Boot-Kernel-Path")" || :
887 boot_initrd_path="$(get_machine_field "$machine" "Boot-Initrd-Path")" || :
888 boot_kernel_path_version="$(get_machine_field "$machine" "Boot-Kernel-Path-Version")" || :
889 boot_initrd_path_version="$(get_machine_field "$machine" "Boot-Initrd-Path-Version")" || :
890 boot_script_path="$(get_machine_field "$machine" "Boot-Script-Path")" || :
891 boot_dtb_path="$(get_machine_field "$machine" "Boot-DTB-Path")" || :
892 boot_dtb_path_version="$(get_machine_field "$machine" "Boot-DTB-Path-Version")" || :
893 boot_multi_path="$(get_machine_field "$machine" "Boot-Multi-Path")" || :
894 android_boot_device="$(get_machine_field "$machine" "Android-Boot-Device")" || :
895
896 if [ -n "$dtb_append_from" ]; then
897 if dtb_append_required; then
898 dtb_append="yes"
899 else
900 dtb_append="no"
901 fi
902 fi
903
904 if [ -n "$mtd_kernel" ] || [ -n "$mtd_initrd" ]; then
905 if [ ! -e "$PROC_MTD" ]; then
906 error "$PROC_MTD doesn't exist"
907 fi
908 fi
909 if [ -n "$mtd_kernel" ]; then
910 kmtd=$(mtdchar "$mtd_kernel")
911 if [ -z "$kmtd" ]; then
912 error "Cannot find mtd partition '$mtd_kernel'"
913 fi
914 check_char_dev "$kmtd"
915 kmtdsize=$(mtdsize "$mtd_kernel")
916 kreqsize=$kfilesize
917 check_mtd_size "$mtd_kernel" $kreqsize $kmtdsize kernel
918 fi
919 if [ -n "$mtd_initrd" ]; then
920 imtd=$(mtdchar "$mtd_initrd")
921 if [ -z "$imtd" ]; then
922 error "Cannot find mtd partition '$mtd_initrd'"
923 fi
924 check_char_dev "$imtd"
925 imtdsize=$(mtdsize "$mtd_initrd")
926 ireqsize=$ifilesize
927 # encapsulating in an U-Boot image grows the size by 64 bytes
928 if [ -n "$uiaddr" ]; then
929 ireqsize=$(($ireqsize + 64))
930 fi
931 check_mtd_size "$mtd_initrd" $ireqsize $imtdsize initrd
932 fi
933
934 tmpdir=""
935 boot_mnt_dir=""
936 cleanups() {
937 rm -rf "$tmpdir"
938 if [ -d "$boot_mnt_dir" ]; then
939 umount "$boot_mnt_dir"
940 rmdir "$boot_mnt_dir"
941 fi
942 }
943 trap cleanups EXIT HUP INT QUIT ILL KILL SEGV PIPE TERM
944 self="$(basename "$0")"
945 tmpdir="$(mktemp -dt "$self.XXXXXXXX")"
946
947
948 case "$method" in
949 "android")
950 [ -n "$ifile" ] || error "Initrd required for android method"
951 part=""
952 largest_size="-1"
953 for p in "$android_boot_device"*[0-9]; do
954 if ! abootimg="$(LC_ALL=C abootimg -i "$p" 2>/dev/null)"; then
955 continue
956 fi
957 image_size="$(abootimg_get_image_size "$abootimg")"
958 if [ -n "$image_size" ] &&
959 [ "$image_size" -gt "$largest_size" ]; then
960 part="$p"
961 fi
962 done
963 if [ -z "$part" ]; then
964 error "Couldn't find Android boot partition on $android_boot_device"
965 fi
966 android_flash "$part"
967 ;;
968 "bootspec")
969 {
970 printf "title\t\tDebian\n"
971 printf "version\t\t$(cat /etc/debian_version) ($desc)\n"
972 printf "linux\t\t$kfile\n"
973 [ -z "$ifile" ] || printf "initrd\t\t$ifile\n"
974 if [ -n "$dtb_name" ] ; then
975 printf "devicetree\t$dtbfile\n"
976 fi
977 printf "options\t\t$(get_kernel_cmdline)\n"
978 printf "linux-appendroot true\n"
979 } > "$tmpdir/debian.conf"
980 mkdir -p /loader/entries
981 mv "$tmpdir/debian.conf" /loader/entries/debian.conf
982 ;;
983 "generic")
984 kernel="$kfile"
985 initrd="$ifile"
986 if [ "$dtb_append" = "yes" ]; then
987 dtb=$(find_dtb_file)
988 append_dtb "$kernel" "$dtb" "$tmpdir/kernel"
989 kernel="$tmpdir/kernel"
990 elif [ -n "$machine_id" ]; then
991 gen_kernel "$kernel" "$tmpdir/kernel" "$machine_id"
992 kernel="$tmpdir/kernel"
993 fi
994 if [ -n "$ukaddr" ]; then
995 if [ -n "$ukepoint" ]; then
996 mkimage_kernel "$ukaddr" "$ukepoint" "$desc" "$kernel" \
997 "$tmpdir/uImage"
998 else
999 mkimage_kernel "$ukaddr" "$ukaddr" "$desc" "$kernel" \
1000 "$tmpdir/uImage"
1001 fi
1002 kernel="$tmpdir/uImage"
1003 rm -f "$tmpdir/kernel"
1004 fi
1005 if [ -n "$umaddr" ]; then
1006 mkimage_multi "$umaddr" "$desc" "$kernel" "$initrd" \
1007 "$tmpdir/uImage"
1008 rm -f "$tmpdir/kernel"
1009 fi
1010 if [ -n "$boot_device" ]; then
1011 check_block_dev "$boot_device"
1012 echo "Will use $boot_device as boot device." >&2
1013 # don't use $tmpdir/boot as to not nuke it accidentally
1014 # if umount fails
1015 boot_mnt_dir="$(mktemp -dt "$self.XXXXXXXX")"
1016 mount "$boot_device" "$boot_mnt_dir"
1017 fi
1018 if [ -n "$boot_kernel_path" ]; then
1019 boot_kernel_path="$boot_mnt_dir/$boot_kernel_path"
1020 if [ "$boot_kernel_path_version" = "yes" ]; then
1021 boot_kernel_path="${boot_kernel_path}-${kvers}"
1022 fi
1023 # don't mv the original kernel
1024 if [ "$kernel" = "$kfile" ]; then
1025 cp "$kernel" "$tmpdir/kernel"
1026 kernel="$tmpdir/kernel"
1027 fi
1028 backup_and_install "$kernel" "$boot_kernel_path"
1029 elif [ -n "$kmtd" ]; then
1030 flash_kernel "$tmpdir/uImage" "$kmtd" ""
1031 rm -f "$tmpdir/uImage"
1032 fi
1033 if [ -n "$boot_multi_path" ]; then
1034 backup_and_install "$tmpdir/uImage" "$boot_multi_path"
1035 fi
1036 if [ -n "$uiaddr" ]; then
1037 [ -n "$ifile" ] || error "Initrd required for generic method with Initrd-Adress"
1038 mkimage_initrd "$uiaddr" "$idesc" "$initrd" \
1039 "$tmpdir/uInitrd"
1040 initrd="$tmpdir/uInitrd"
1041 fi
1042 if [ -n "$boot_initrd_path" ]; then
1043 [ -n "$ifile" ] || error "Initrd required for generic method with Boot-Initrd-Path"
1044 boot_initrd_path="$boot_mnt_dir/$boot_initrd_path"
1045 if [ "$boot_initrd_path_version" = "yes" ]; then
1046 boot_initrd_path="${boot_initrd_path}-${kvers}"
1047 fi
1048 # don't mv the original initrd
1049 if [ "$initrd" = "$ifile" ]; then
1050 cp "$initrd" "$tmpdir/initrd"
1051 initrd="$tmpdir/initrd"
1052 fi
1053 backup_and_install "$initrd" "$boot_initrd_path"
1054 elif [ -n "$imtd" ]; then
1055 [ -n "$ifile" ] || error "Initrd required for generic method with Mtd-Initrd"
1056 ipad=0
1057 # padding isn't needed for U-Boot images
1058 if [ -z "$uiaddr" ]; then
1059 ipad=$(($imtdsize - $ireqsize))
1060 fi
1061 flash_initrd "$initrd" "$imtd" $ipad
1062 rm -f "$tmpdir/uInitrd"
1063 fi
1064 if [ -n "$boot_script_path" ]; then
1065 boot_script_path="$boot_mnt_dir/$boot_script_path"
1066 boot_script="$tmpdir/bootscript"
1067 for script in $usname ; do
1068 echo "\n#\n# flash-kernel: $script\n#\n" >> "$boot_script"
1069 cat "$BOOTSCRIPTS_DIR/$script" >> "$boot_script"
1070 done
1071 mkimage_script "$usaddr" "boot script" "$boot_script" \
1072 "$tmpdir/boot.scr"
1073 boot_script="$tmpdir/boot.scr"
1074 backup_and_install "$boot_script" "$boot_script_path"
1075 fi
1076 if [ -n "$boot_dtb_path" ] && [ "$dtb_append" != "no" ]; then
1077 boot_dtb_path="$boot_mnt_dir/$boot_dtb_path"
1078 if [ "$boot_dtb_path_version" = "yes" ]; then
1079 boot_dtb_path="${boot_dtb_path}-${kvers}"
1080 fi
1081 boot_dtb=$(find_dtb_file)
1082 dtb="$tmpdir/dtb"
1083 cp "$boot_dtb" "$dtb"
1084 backup_and_install "$dtb" "$boot_dtb_path"
1085 fi
1086 ;;
1087 "symlink")
1088 [ -n "$ifile" ] || error "Initrd required for symlink method"
1089 rm -f /boot/initrd /boot/zImage
1090 ln -s "$(basename "$ifile")" /boot/initrd
1091 gen_kernel "$kfile" "/boot/zImage" "$machine_id"
1092 ;;
1093 "multi")
1094 [ -n "$ifile" ] || error "Initrd required for multi method"
1095 gen_kernel "$kfile" "$tmpdir/kernel" ""
1096 # Hack to work around a bug in some U-Boot versions:
1097 if [ $(($(stat -c '%s' "$tmpdir/kernel") % 4)) -eq 0 ]; then
1098 echo >> "$tmpdir/kernel"
1099 fi
1100 mkimage_multi "$umaddr" "$desc" "$tmpdir/kernel" "$ifile" \
1101 "$tmpdir/uImage"
1102 rm -f "$tmpdir/kernel"
1103 backup_and_install "$tmpdir/uImage" "$boot_multi_path"
1104 ;;
1105 "redboot")
1106 [ -n "$ifile" ] || error "Initrd required for redboot method"
1107 flash_kernel "$kfile" "$kmtd" "$machine_id"
1108 pad=$(($imtdsize - $ifilesize))
1109 flash_initrd "$ifile" "$imtd" $pad
1110 ;;
1111 "slug")
1112 [ -n "$ifile" ] || error "Initrd required for slug method"
1113 case "$(dpkg --print-architecture)" in
1114 arm|armel)
1115 little_endian=1
1116 ;;
1117 armeb)
1118 little_endian=0
1119 ;;
1120 esac
1121 mtd_fis="FIS directory"
1122 fismtd=$(mtdblock "$mtd_fis")
1123 if [ -z "$fismtd" ]; then
1124 error "Cannot find mtd partition '$mtd_fis'"
1125 fi
1126 check_mtd_size "$mtd_kernel" $(($kfilesize + 16 + 16)) \
1127 $kmtdsize kernel
1128 check_mtd_size "$mtd_initrd" $(($ifilesize + 16)) \
1129 $imtdsize initrd
1130 # The following devio magic parses the FIS directory to
1131 # obtain the size, offset and name of each partition. This
1132 # used used to obtain the offset of the Kernel partition.
1133 offset=$(echo "$(devio "<<$fismtd" '
1134 <= $ 0x20000 -
1135 L= 0x1000
1136 $( 1
1137 # 0xff byte in name[0] ends the partition table
1138 $? @ 255 =
1139 # output size base name
1140 <= f15+
1141 .= b 0xfffffff &
1142 <= f4+
1143 .= b
1144 pf "%lu %lu "
1145 <= f28-
1146 cp 16
1147 pn
1148 <= f240+
1149 L= L256-
1150 $) L255>')" |
1151 while read a b c; do
1152 if [ "$c" = "Kernel" ]; then
1153 echo $b
1154 fi
1155 done)
1156 # The Kernel partition, starting at $offset, is divided into
1157 # two areas at $boundary. We therefore need to split the
1158 # kernel into two and write them to flash with two Sercomm
1159 # headers.
1160 boundary=1441792 # 0x00160000
1161 ksize1=$(($boundary - $offset - 16))
1162 printf "Flashing kernel: " >&2
1163 {
1164 sercomm_header $(($kfilesize + 16))
1165 dd if="$kfile" of="$tmpdir/kpart1" bs=$ksize1 \
1166 count=1 2>/dev/null
1167 nslu2_swap "$tmpdir/kpart1"
1168 rm -f "$tmpdir/kpart1"
1169 sercomm_header 131072
1170 dd if="$kfile" of="$tmpdir/kpart2" ibs=$ksize1 \
1171 skip=1 2>/dev/null
1172 nslu2_swap "$tmpdir/kpart2"
1173 rm -f "$tmpdir/kpart2"
1174 } > "$kmtd" || error "failed."
1175 echo "done." >&2
1176 printf "Flashing initramfs: " >&2
1177 dd if="$ifile" of="$tmpdir/initrd" ibs=$(($imtdsize - 16)) \
1178 conv=sync 2>/dev/null
1179 {
1180 sercomm_header $ifilesize
1181 nslu2_swap "$tmpdir/initrd"
1182 rm -f "$tmpdir/initrd"
1183 } > "$imtd" || error "failed."
1184 echo "done." >&2
1185 ;;
1186 "olpc")
1187 [ -n "$ifile" ] || error "Initrd required for olpc method"
1188 local olpcfth="$(mktemp --tmpdir=$tmpdir)"
1189 local olpc_boot_dir="$(findmnt -n --output target --target /boot)"
1190 local kernel="$(realpath --relative-to=$olpc_boot_dir $kfile |sed 's!/!\\\\!')"
1191 local initrd="$(realpath --relative-to=$olpc_boot_dir $ifile |sed 's!/!\\\\!')"
1192
1193 printf "Generating olpc.fth... " >&2
1194 sed -e "s!@@KERNEL@@!$kernel!g" \
1195 -e "s!@@INITRD@@!$initrd!g" \
1196 -e "s!@@LINUX_KERNEL_CMDLINE@@!$(get_kernel_cmdline)!g" \
1197 -e "s!@@LINUX_KERNEL_CMDLINE_DEFAULTS@@!$(get_kernel_cmdline_defaults)!g" \
1198 < "$BOOTSCRIPTS_DIR/olpc.fth" > $olpcfth
1199 mkdir -p "$olpc_boot_dir/boot"
1200 echo "done." >&2
1201
1202 backup_and_install "$olpcfth" "$olpc_boot_dir/boot/olpc.fth"
1203 ;;
1204 esac
1205 }
1206
1207 # vim:noexpandtab shiftwidth=8 syntax=sh