1#!/bin/bash
2
3# Provide source directive to shellcheck.
4# shellcheck source=meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw/mori-lib.sh
5# Disable check for globbing and word splitting within double quotes
6# shellcheck disable=SC2086
7source /usr/libexec/mori-fw/mori-lib.sh
8
9function fwbios() {
10  ret=0
11  if [ ! -f "$1" ]; then
12    echo " Cannot find the" "$1" "image file"
13    return 1
14  fi
15
16  setup_bios_access
17
18  # write to the mtd device
19  BIOS_MTD=$(grep "hnor" /proc/mtd | sed -n 's/^\(.*\):.*/\1/p')
20  {
21  flock -x 200
22  echo "Flashing BIOS @/dev/${BIOS_MTD}"
23  if ! flashcp -v $1 /dev/${BIOS_MTD} ; then
24    echo "Flashing the bios failed " >&2
25    ret=1
26  fi
27  } 200>$RST_LOCK_FILE
28
29  cleanup_bios_access
30
31  return $ret
32}
33
34function fwbmccpld() {
35  ret=0
36  if [ ! -s "$1" ]; then
37    echo "Image file" "$1" "is empty or nonexistent, BMC CPLD update failed"
38    return 1
39  fi
40
41  # MB_JTAG_MUX 0:CPU 1:MB
42  # BMC_JTAG_MUX 0:GF/MB 1:BMC
43  set_gpio_ctrl MB_JTAG_MUX_SEL 0
44  set_gpio_ctrl BMC_JTAG_MUX_SEL  1
45
46  {
47  flock -x 200
48  # 1st condition checks if the svf file is valid
49  # 2nd condition checks flashing logs for flash errors
50  if ! mesg=$(loadsvf -d /dev/jtag0 -s $1 -m 0 2>&1) \
51        || echo "$mesg" | grep -i -e error -e fail ; then
52    echo "$mesg" | grep -i -e error -e fail
53    echo "BMC CPLD update failed"
54    ret=1
55  else
56    echo "BMC CPLD update successful"
57  fi
58  } 200>$RST_LOCK_FILE
59  return $ret
60}
61
62function fwmbcpld() {
63  ret=0
64  if [ ! -s "$1" ]; then
65    echo "Image file" "$1" "is empty or nonexistent, MB CPLD update failed"
66    return 1
67  fi
68
69  # MB_JTAG_MUX 0:CPU 1:MB
70  # BMC_JTAG_MUX 0:GF/MB 1:BMC
71  set_gpio_ctrl MB_JTAG_MUX_SEL 1
72  set_gpio_ctrl BMC_JTAG_MUX_SEL  0
73
74  {
75  flock -x 200
76  # 1st condition checks if the svf file is valid
77  # 2nd condition checks flashing logs for flash errors
78  if ! mesg=$(loadsvf -d /dev/jtag0 -s $1 -m 0 2>&1) \
79        || echo "$mesg" | grep -i -e error -e fail ; then
80    echo "MB CPLD update failed"
81  ret=1
82  else
83    echo "MB CPLD update successful"
84  fi
85  } 200>$RST_LOCK_FILE
86  set_gpio_ctrl MB_JTAG_MUX_SEL 0
87  echo "MB CPLD update successful"
88
89  return $ret
90}
91
92function fwbootstrap() {
93  # to flash the CPU EEPROM
94  #unbind bootstrap EEPROM
95  echo ${I2C_CPU_EEPROM[0]}-00${I2C_CPU_EEPROM[1]} > /sys/bus/i2c/drivers/at24/unbind
96
97  #switch access to BMC
98  set_gpio_ctrl CPU_EEPROM_SEL 0
99
100  if [ "$(bootstrap_flash -b ${I2C_CPU_EEPROM[0]} -s 0x${I2C_CPU_EEPROM[1]} -p -f $1)" -ne  0 ]; then
101    echo "CPU bootstrap EEPROM update failed" >&2
102    return 1
103  fi
104  wait
105
106  #bind bootstrap EEPROM
107  echo ${I2C_CPU_EEPROM[0]}-00${I2C_CPU_EEPROM[1]} > /sys/bus/i2c/drivers/at24/bind
108
109  #switch back access to CPU
110  set_gpio_ctrl CPU_EEPROM_SEL 1
111  return 0
112
113}
114
115function fwmb_pwr_seq(){
116  #$1 PS seq config file
117  ret=0
118  if [[ ! -e "$1" ]]; then
119    echo "The file $1 does not exist"
120    return 1
121  fi
122  echo "${I2C_MB_PWRSEQ[0]}"-00"${I2C_MB_PWRSEQ[1]}" > /sys/bus/i2c/drivers/adm1266/unbind
123  {
124  flock -x 200
125  #Parameters passed to adm1266_fw_fx to be used to flash PS
126  #1st I2C bus number of PS's
127  #2nd PS seq config file
128  if [ "$(mb_power_sequencer_flash ${I2C_MB_PWRSEQ[0]} $1)" -ne  0 ]; then
129
130    echo "The power seq flash failed" >&2
131    ret=1
132  else
133    echo "${I2C_MB_PWRSEQ[0]}"-00"${I2C_MB_PWRSEQ[1]}" > /sys/bus/i2c/drivers/adm1266/bind
134  fi
135  } 200>$RST_LOCK_FILE
136
137  return $ret
138}
139
140if [[ ! $(which flashcp) ]]; then
141    echo "flashcp utility not installed"
142    exit 1
143fi
144if [[ ! $(which loadsvf) ]]; then
145    echo "loadsvf utility not installed"
146    exit 1
147fi
148if [[ ! $(which mb_power_sequencer_flash) ]]; then
149    echo "mb_power_sequencer_flash utility not installed"
150    exit 1
151fi
152if [[ ! $(which bootstrap_flash) ]]; then
153    echo "bootstrap_flash utility not installed"
154    exit 1
155fi
156if [[ ! -e /dev/jtag0 ]]; then
157    echo "Jtag device driver not functional"
158    exit 1
159fi
160
161case $1 in
162  bios)
163    fwbios "$2"
164    ;;
165  bmccpld)
166    fwbmccpld "$2"
167    ;;
168  mbcpld)
169    fwmbcpld "$2"
170    ;;
171  bootstrap)
172    fwbootstrap "$2"
173    ;;
174  mbseq)
175    fwmb_pwr_seq "$2"
176    ;;
177  *)
178    ;;
179esac
180ret=$?
181
182rm -f "$2"
183
184exit $ret
185