blob: 2b3796bfaee763e9dd6e51b5e8877df66b7a19bb [file] [log] [blame]
# Copyright 2018 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
description "Job to generate rdc_update.bin"
author "chromium-os-dev@chromium.org"
start on starting cras
task
env CRAS_RUN_DIR="/run/cras"
# Note: cannot use ${CRAS_RUN_DIR}, will not expand.
# Known issue: https://bugs.launchpad.net/upstart/+bug/328366
env DIST_FILE="/run/cras/rdc_update.bin"
env DEFAULT_RDC_FILE="/opt/google/dsm/rdc_update.default"
env VALID_FROM="0x045F202C"
env VALID_TO="0x15DBA0DF"
script
dlog() {
logger -t generate_rdc_update -p debug "$1"
}
use_default() {
dlog "something went wrong, using default rdc_update.bin"
cp "${DEFAULT_RDC_FILE}" "${DIST_FILE}"
chown cras:cras "${DIST_FILE}"
chmod 440 "${DIST_FILE}"
exit 0
}
set +e
# cras.conf will do this again.
mkdir -p -m 1770 "${CRAS_RUN_DIR}"
chown cras:cras "${CRAS_RUN_DIR}"
dlog "reading dsm_calib from VPD..."
calib=$(vpd_get_value dsm_calib)
if [ "$?" -ne 0 ]; then
use_default
fi
dlog "reading dsm_calib from VPD...ok, dsm_calib=${calib}"
dlog "checking format..."
echo "${calib}" | awk '{
if (NF != 6)
exit 1
if (length($1) != 7 && length($1) != 8)
exit 1
for (i = 2; i <= NF; ++i)
if (length($i) != 8)
exit 1
# check for hex-string characters
if (match($0, /[^0-9a-fA-F ]/))
exit 1
}'
if [ "$?" -ne 0 ]; then
use_default
fi
dlog "checking format...ok"
dlog "checking boundary..."
valid_from=$(printf "%d" "${VALID_FROM}")
valid_to=$(printf "%d" "${VALID_TO}")
left=$(printf "%d" "0x$(echo "${calib}" | awk '{print $4}')")
right=$(printf "%d" "0x$(echo "${calib}" | awk '{print $6}')")
if [ "${left}" -lt "${valid_from}" -o "${left}" -gt "${valid_to}" -o \
"${right}" -lt "${valid_from}" -o "${right}" -gt "${valid_to}" ]; then
use_default
fi
dlog "checking boundary...ok"
dlog "generating little endian byte sequence for each dword..."
bs=$(echo "${calib}" | awk '{
# Prepend "0" if the first sequence has only 7 characters.
if (length($1) == 7)
$1 = "0" $1
for (i = 1; i <= NF; ++i) {
# the index for substr start from 1
high = substr($i, 1, 4)
high_1 = substr(high, 1, 2)
high_2 = substr(high, 3, 2)
low = substr($i, 5, 4)
low_1 = substr(low, 1, 2)
low_2 = substr(low, 3, 2)
printf "\\x%s\\x%s\\x%s\\x%s", low_2, low_1, high_2, high_1
}
}')
if [ "$?" -ne 0 ]; then
use_default
fi
dlog "generating little endian byte sequence for each dword...ok, \
byte sequence=${bs}"
# Built-in printf of /bin/sh does not support \x, use the external one.
/usr/bin/printf "%b" "${bs}" > "${DIST_FILE}"
chown cras:cras "${DIST_FILE}"
chmod 440 "${DIST_FILE}"
end script