#!/bin/sh
# test we are root
if [ "$(id -u)" != "0" ]; then
   echo "run as root now exitting"
   exit 1
fi

# no flags its a get script

P=microcodes-get-amd
V=1
USER=`cat /etc/sysconfig/tcuser`
su -c "tce-load -i submitqc" $USER

# main
######
cd /tmp
mkdir -p $P/usr/local/bin 
echo '#!/bin/sh
rm -rf *amd*bin kernel # clean tmp of any microcodes and any old kernel dir
DIR=kernel/x86/microcode && mkdir -p $DIR && cd $DIR
URL=https://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git/tree/amd-ucode
M=microcode_amd_fam
LIST="15h 16h 17h 19h "
for Z in $LIST
do
/usr/local/bin/wget -nc --no-check-certificate $URL/$M$Z.bin    
done

# older micro
/usr/local/bin/wget -nc --no-check-certificate $URL/microcode_amd.bin

echo microcode_amd* > AuthenticAMD.bin
echo AuthenticAMD.bin | bsdcpio -o -H newc -R 0:0 > /tmp/amd-ucode.img
file /tmp/amd-ucode.img ' > $P/usr/local/bin/$P.sh
cd /tmp
chmod 755 $P/usr/local/bin/$P.sh

# tce.install
#############
mkdir -p $P/usr/local/tce.installed
echo '#!/bin/sh
P=microcodes-get-amd
[ ! -x /usr/local/bin/$P.sh ] || /usr/local/bin/$P.sh ' > $P/usr/local/tce.installed/$P 
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

mkdir -p $P/usr/local/share/doc/$P
echo 'open source' > $P/usr/local/share/doc/$P/COPYING

# TCZ them
###########
LIST2="$P  "
for Z in $LIST2
do
	mksquashfs $Z $Z.tcz
	md5sum $Z.tcz > $Z.tcz.md5.txt
	cd $Z
	find . -not -type d | cut -c 2- | sort > /tmp/$Z.tcz.list
	cd /tmp
done

ls -hal

echo "Title:	          microcodes-get-amd.tcz
Description:    get script to make early loading AMD microcodes
Version:        1
Author:         aus9 @ linuxquestions.org 
Original-site:  https://tinyurl.com/amdfirmware                
Copying-policy: open source
Size:           4.0K
Extension-by:   aus9 @ linuxquestions.org
Tags:           amd microcodes bios
Comments:       Based on my forum post 2023 in General TC Talk
                https://tinyurl.com/amdmicocodes but added an older microcode. 

                Its a get script and the resultant amd-ucode.img will be found at /tmp
                Loading the TCE will execute the script so please do NOT 
                have this in your boot list. Late loading microcodes are forbidden.               

                Before loading this TCE run $ dmesg | grep microcode
                and remember that value. Next load this TCE and copy that 
                amd-code.img to your boot Dir and adjust your grub.cfg at 
                the initrd line so it reads
                
                initrd /boot/amd-ucode.img (before your core or rootfs stuff)
 
                Full reboot and then recheck $ dmesg | grep microcode 
                to see if value changes. An updated bios should not have
                a changed value eg mine is unchanged 0x0a500014 

                An updated bios is better, but not all makers offer bios
                updates for older PCs. 

                Script has no idea if there a new updates so check  
                https://tinyurl.com/amdfirmware
                Forum post reply 2...discusses making a specific microcode script
                if interested.
      
Change-log:     2026/10/23 v 1 on 17x 
Current:        2026/10/23 " > $P.tcz.info

$ 64 bit dep 
echo 'file.tcz
libarchive.tcz
wget.tcz ' > $P.tcz.dep

# amend dep to have a libarchive3 for 32 bit....not showing here

submitqc --libs
rm -rf *.zsync



