1#!/bin/bash
2
3# Provide source directive to shellcheck.
4# shellcheck source=meta-fii/meta-kudo/recipes-kudo/kudo-fw-utility/kudo-fw/kudo-lib.sh
5# Disable check for globbing and word splitting within double quotes
6# shellcheck disable=SC2086
7source /usr/libexec/kudo-fw/kudo-lib.sh
8
9function fwbios() {
10  KERNEL_FIU_ID="c0000000.spi"
11  KERNEL_SYSFS_FIU="/sys/bus/platform/drivers/NPCM-FIU"
12
13  # switch the SPI mux from Host to BMC
14  i2cset -y -f -a ${I2C_BMC_CPLD[0]} 0x${I2C_BMC_CPLD[1]} 0x10 0x01
15
16  # rescan the spi bus
17  if [ -d "${KERNEL_SYSFS_FIU}/${KERNEL_FIU_ID}" ]; then
18    echo "${KERNEL_FIU_ID}" > "${KERNEL_SYSFS_FIU}"/unbind
19    sleep 1
20  fi
21  echo "${KERNEL_FIU_ID}" > "${KERNEL_SYSFS_FIU}"/bind
22
23  # write to the mtd device
24  BIOS_MTD=$(grep "bios" /proc/mtd | sed -n 's/^\(.*\):.*/\1/p')
25
26  if [ ! -f "$1" ]; then
27    echo " Cannot find the" "$1" "image file"
28    return 1
29
30  fi
31  echo "Flashing BIOS @/dev/${BIOS_MTD}"
32  if [ "$(flashcp -v $1 /dev/${BIOS_MTD})" -ne  0 ]; then
33    echo "Flashing the bios failed " >&2
34    return 1
35  fi
36  wait
37
38  # switch the SPI mux from BMC to Host
39  if [ -d "${KERNEL_SYSFS_FIU}/${KERNEL_FIU_ID}" ]; then
40    echo "${KERNEL_FIU_ID}" > "${KERNEL_SYSFS_FIU}"/unbind
41  fi
42  i2cset -y -f -a ${I2C_BMC_CPLD[0]} 0x${I2C_BMC_CPLD[1]} 0x10 0x00
43
44  # Disable LPI mode NV_SI_CPU_LPI_FREQ_DISABLE for SCP 1.06 and older.
45  if [ "$(nvparm -s 0x1 -o 0x114090)" -ne  0 ]; then
46    echo "Setting LPI mode for SCP 1.06 and older failed " >&2
47    return 1
48  fi
49
50  # Disable LPI mode NV_SI_CPU_LPI_FREQ_DISABLE for SCP 1.07 and newer
51  if [ "$(nvparm -s 0x1 -o 0x02A8)" -ne  0 ]; then
52    echo "Setting LPI mode for SCP 1.07 and newer failed " >&2
53    return 1
54  fi
55
56  # Disable toggling of SMPro heartbeat
57  if [ "$(nvparm -s 0x0 -o 0x5F0638)" -ne  0 ]; then
58    echo "Setting SMpro heartbeat failed " >&2
59    return 1
60  fi
61
62  if [[ $(find "$1" -type f -size +17156k 2>/dev/null) ]]; then
63    echo "Extracting the SCP from the image"
64    dd if="$1" bs=1024 skip=17156 count=256 of=/run/initramfs/myscp.img
65    # Update both primary and backup EEPROM
66    fwscp /run/initramfs/myscp.img
67    fwscpback /run/initramfs/myscp.img
68  fi
69
70
71  return 0
72}
73
74function fwbmccpld() {
75  # BMC_JTAG_MUX_1 0:BMC 1:MB
76  set_gpio_ctrl MB_JTAG_MUX_SEL 0
77  if [ "$(loadsvf -d /dev/jtag0 -s $1 -m 0)" -ne  0 ]; then
78    echo "BMC CPLD update failed" >&2
79    return 1
80  fi
81  wait
82  set_gpio_ctrl MB_JTAG_MUX_SEL 1
83
84  return 0
85}
86
87function fwmbcpld() {
88  # BMC_JTAG_MUX_1 0:BMC 1:MB
89  # BMC_JTAG_SEL 0:BMC 1:CPU
90  set_gpio_ctrl MB_JTAG_MUX_SEL 1
91  set_gpio_ctrl BMC_JTAG_MUX_SEL  1
92  if [ "$(loadsvf -d /dev/jtag0 -s $1 -m 0)" -ne  0 ]; then
93    echo "Mobo CPLD update failed" >&2
94    return 1
95  fi
96  wait
97
98  return 0
99}
100
101function fwscp() {
102  # BMC_I2C_BACKUP_SEL 0:failover, 1:main
103  # BMC_CPU_EEPROM_I2C_SEL 0:BMC, 1:CPU
104  scp_eeprom_sel=$(get_gpio_ctrl BACKUP_SCP_SEL)
105  set_gpio_ctrl BACKUP_SCP_SEL 1
106  set_gpio_ctrl CPU_EEPROM_SEL 0
107  if [ "$(ampere_eeprom_prog -b ${I2C_CPU_EEPROM[0]} -s 0x${I2C_CPU_EEPROM[1]} -p -f $1)" -ne  0 ]; then
108    echo "SCP eeprom update failed" >&2
109    return 1
110  fi
111  wait
112  set_gpio_ctrl CPU_EEPROM_SEL 1
113  set_gpio_ctrl BACKUP_SCP_SEL "$scp_eeprom_sel"
114
115  return 0
116}
117
118function fwscpback() {
119  # BMC_I2C_BACKUP_SEL 0:failover, 1:main
120  # BMC_CPU_EEPROM_I2C_SEL 0:BMC, 1:CPU
121  scp_eeprom_sel=$(get_gpio_ctrl BACKUP_SCP_SEL)
122  set_gpio_ctrl BACKUP_SCP_SEL 0
123  set_gpio_ctrl CPU_EEPROM_SEL 0
124  if [ "$(ampere_eeprom_prog -b ${I2C_CPU_EEPROM[0]} -s 0x${I2C_CPU_EEPROM[1]} -p -f $1)" -ne  0 ]; then
125    echo "SCP BACKUP eeprom update failed" >&2
126    return 1
127  fi
128  wait
129  set_gpio_ctrl CPU_EEPROM_SEL 1
130  set_gpio_ctrl BACKUP_SCP_SEL "$scp_eeprom_sel"
131
132  return 0
133}
134
135function fwmb_pwr_seq(){
136  #$1 0x40 seq config file
137  #$2 0x41 seq config file
138  if [[ ! -e "$1" ]]; then
139    echo "The file $1 does not exist"
140    return 1
141  fi
142  if [[ ! -e "$2" ]]; then
143    echo "The file $2 file does not exist"
144    return 1
145  fi
146  echo ${I2C_MB_PWRSEQ1[0]}-00${I2C_MB_PWRSEQ1[1]} > /sys/bus/i2c/drivers/adm1266/unbind
147  echo ${I2C_MB_PWRSEQ2[0]}-00${I2C_MB_PWRSEQ2[1]} > /sys/bus/i2c/drivers/adm1266/unbind
148  #Parameters passed to adm1266_fw_fx to be used to flash PS
149  #1st I2C bus number of PS's
150  #2nd/3rd PS seq config file
151  if [ "$(adm1266_fw_fx ${I2C_MB_PWRSEQ1[0]} $1 $2)" -ne  0 ]; then
152    echo "The power seq flash failed" >&2
153    return 1
154  fi
155  echo ${I2C_MB_PWRSEQ1[0]}-00${I2C_MB_PWRSEQ1[1]} > /sys/bus/i2c/drivers/adm1266/bind
156  echo ${I2C_MB_PWRSEQ2[0]}-00${I2C_MB_PWRSEQ2[1]} > /sys/bus/i2c/drivers/adm1266/bind
157
158  return 0
159}
160
161if [[ ! $(which flashcp) ]]; then
162    echo "flashcp utility not installed"
163    exit 1
164fi
165if [[ ! $(which ampere_eeprom_prog) ]]; then
166    echo "ampere_eeprom_prog utility not installed"
167    exit 1
168fi
169if [[ ! $(which loadsvf) ]]; then
170    echo "loadsvf utility not installed"
171    exit 1
172fi
173if [[ ! -e /dev/jtag0 ]]; then
174    echo "Jtag device driver not functional"
175    exit 1
176fi
177
178case $1 in
179  bios)
180    fwbios "$2"
181    ;;
182  bmccpld)
183    fwbmccpld "$2"
184    ;;
185  mbcpld)
186    fwmbcpld "$2"
187    ;;
188  scp)
189    fwscp "$2"
190    ;;
191  scpback)
192    fwscpback "$2"
193    ;;
194  mbseq)
195    fwmb_pwr_seq "$2" "$3"
196    ;;
197  *)
198    ;;
199esac
200ret=$?
201
202rm -f "$2" "$3"
203
204exit $ret
205