1 #!/bin/ksh
   2 
   3 # This file and its contents are supplied under the terms of the
   4 # Common Development and Distribution License ("CDDL"), version 1.0.
   5 # You may only use this file in accordance with the terms of version
   6 # 1.0 of the CDDL.
   7 #
   8 # A full copy of the text of the CDDL should have accompanied this
   9 # source. A copy of the CDDL is also available via the Internet at
  10 # http://www.illumos.org/license/CDDL.
  11 
  12 # Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
  13 # Copyright 2019 Joyent, Inc.
  14 
  15 # A simple update script that extracts an Intel microcode download file
  16 # into the intel/ directory, and updates the hardlinks in the
  17 # system/kernel/platform manifest.
  18 
  19 set -e
  20 set -o pipefail
  21 
  22 [[ -z "$1" ]] || [[ ! -f "$1" ]] && {
  23         echo "Syntax: $0 <path to microcode tar>" >&2
  24         exit 1
  25 }
  26 
  27 ucodetar="$1"
  28 
  29 mf=../../pkg/manifests/system-kernel-platform.mf
  30 [[ -f $mf ]] || {
  31         echo "Run from usr/src/data/ucode" 2>&1
  32         exit 1
  33 }
  34 
  35 fw=platform/i86pc/ucode/GenuineIntel
  36 
  37 tmp=$(mktemp -d)
  38 mkdir $tmp/out
  39 
  40 gtar -C $tmp -xvf "$ucodetar"
  41 
  42 find $tmp/Intel-Linux-Processor-Microcode-Data*/intel-ucode*/ -type f \
  43     | while read f; do
  44         echo "Converting $(basename $f)"
  45         cp $f $tmp/intel-fw
  46         ucodeadm -i -R $tmp/out $tmp/intel-fw
  47         rm -f $tmp/intel-fw
  48 done
  49 
  50 pkgfmt -u $mf
  51 mv $mf $mf.tmp
  52 egrep -v "(file|hardlink) path=$fw" $mf.tmp > $mf
  53 rm -f $mf.tmp
  54 
  55 rm -f intel/*
  56 rm -f Makefile.links
  57 
  58 typeset -A seen
  59 typeset -A inodes
  60 typeset -A links
  61 
  62 for f in $tmp/out/*; do
  63         bf=$(basename $f)
  64         [[ -n "${seen[$bf]}" ]] && continue
  65         inode=$(stat -c %i $f)
  66         if [[ -n "${inodes[$inode]}" ]]; then
  67                 links[$bf]=${inodes[$inode]}
  68         else
  69                 inodes[$inode]=$bf
  70                 seen[$bf]=1
  71                 cp $f intel/$bf
  72         fi
  73         seen[$bf]=1
  74 done
  75 
  76 for f in intel/*; do
  77         bf=$(basename $f)
  78         echo "\$(i386_ONLY)file path=$fw/$bf group=sys mode=0444 reboot-needed=true" >> $mf
  79 done
  80 
  81 echo "INTEL_LINKS = ${!links[@]}" > Makefile.links
  82 echo >> Makefile.links
  83 
  84 for i in "${!links[@]}"; do
  85         echo "\$(i386_ONLY)hardlink path=$fw/$i target=${links[$i]}" >> $mf
  86         cat << EOM >> Makefile.links
  87 \$(ROOTINTELDIR)/$i: \$(ROOTINTELDIR)/${links[$i]}
  88         \$(RM) \$@; \$(LN) \$^ \$@
  89 
  90 EOM
  91 done
  92 
  93 pkgfmt $mf
  94 
  95 rm -rf $tmp