Skip to content

Commit

Permalink
installer: improve RTC handling (#188)
Browse files Browse the repository at this point in the history
* Add 'abx80x' to list of possible RTC drivers

* Add 'module_enable' function in installer script for modules to be loaded
  in installed system

* Use 'dtoverlay_enable' function in various places it was not being used

* Ensure module for the RTC has been loaded during installation

* Stop adding 'hwclock' call to /etc/rc.local as it is no longer needed
  - systems using sysvinit already have a udev rule to do this
  - systems using systemd will use service unit described below

* Add systemd service unit to read RTC on startup
  - as seen in Debian bug 855203, systems using systemd don't read the RTC
    during startup if the RTC's driver is loaded as a module; since all
    Raspberry Pi kernels are configured this way, a service unit is required
    to read the RTC during system startup

* Ensure module for the RTC will be loaded in installed system

* Ensure that RTC date/time is set in UTC mode

The 'ensure module loaded' bits are required because at least one RTC module
(abx80x) is not automatically loaded even though the proper Device Tree overlay
has been configured. For those which are autoloaded, having the module name
in /etc/modules (or /etc/modules-load.d/rtc.conf) won't cause any harm.

Signed-off-by: Kevin P. Fleming <[email protected]>
  • Loading branch information
kpfleming authored Mar 18, 2020
1 parent 43d94ea commit caf7423
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
2 changes: 1 addition & 1 deletion doc/INSTALL_CUSTOM.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Note that if the networking configuration is set to use DHCP, no additional pack
| `sound_usb_first` | `0` | `0`/`1` | Set to "1" to define USB audio as default if onboard audio is also enabled. The options `sound_enable=1` and `sound_usb_enable=1` have to be set to take effect. |
| `camera_enable` | `0` | `0`/`1` | Set to "1" to enable the camera module. This enables all camera-related parameters in config.txt. |
| `camera_disable_led` | `0` | `0`/`1` | Disables the camera LED. The option `camera_enable=1` has to be set to take effect. |
| `rtc` | | `ds1307`/ `ds1339`/ `ds3231`/ `mcp7940x`/ `mcp7941x`/ `pcf2127`/ `pcf8523`/ `pcf8563` | Select an RTC if it is connected via I²C. |
| `rtc` | | `ds1307`/ `ds1339`/ `ds3231`/ `mcp7940x`/ `mcp7941x`/ `pcf2127`/ `pcf8523`/ `pcf8563`/ `abx80x` | Select an RTC if it is connected via I²C. |
| `dt_overlays` | | | Enables additional device tree overlays (comma separated and quoted). (e.g. 'dt_overlays="hifiberry-dac,lirc-rpi"') |

## SSH
Expand Down
60 changes: 44 additions & 16 deletions scripts/opt/raspberrypi-ua-netinst/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,16 @@ dtoverlay_enable() {
fi
}

module_enable() {
local module="${1}"
local purpose="${2}"
if [ "${init_system}" = "systemd" ]; then
echo "${module}" > "/rootfs/etc/modules-load.d/${purpose}.conf"
else
echo "${module}" >> /rootfs/etc/modules
fi
}

#######################
### INSTALLER ###
#######################
Expand Down Expand Up @@ -755,7 +765,7 @@ fi
if [ -n "${rtc}" ] ; then
echo -n " Enabling RTC configuration... "
if ! grep -q "^dtoverlay=i2c-rtc,${rtc}\>" /boot/config.txt; then
echo -e "\ndtoverlay=i2c-rtc,${rtc}" >> /boot/config.txt
dtoverlay_enable "/boot/config.txt" "i2c-rtc,${rtc}"
preinstall_reboot=1
fi
echo "OK"
Expand Down Expand Up @@ -913,6 +923,17 @@ if [ -n "${drivers_to_load}" ]; then
echo
fi

if [ -n "${rtc}" ] ; then
echo -n "Ensuring RTC module has been loaded... "
modprobe "rtc-${rtc}" || fail
echo "OK"
echo -n "Checking hardware clock access... "
mdev -s
sleep 3s
/opt/busybox/bin/hwclock --show &> /dev/null || fail
echo "OK"
fi

echo -n "Waiting for ${ifname}... "
for i in $(seq 1 "${installer_networktimeout}"); do
if ifconfig "${ifname}" &> /dev/null; then
Expand Down Expand Up @@ -1391,13 +1412,6 @@ for i in $(seq 1 5); do
done
echo

if [ -n "${rtc}" ] ; then
echo -n "Checking hardware clock access... "
/opt/busybox/bin/hwclock --show &> /dev/null || fail
echo "OK"
echo
fi

# fdisk's boot offset is 2048, so only handle $bootoffset is it's larger then that
if [ -n "${bootoffset}" ] && [ "${bootoffset}" -gt 2048 ]; then
emptyspaceend=$((bootoffset - 1))
Expand Down Expand Up @@ -2025,8 +2039,25 @@ if [ "${use_systemd_services}" = "0" ]; then
sed -i "s/^\(exit 0\)/\/sbin\/hwclock --hctosys\n\1/" /rootfs/etc/rc.local
fi
else
# enable systemd-timesyncd
ln -s /lib/systemd/system/systemd-timesyncd.service /rootfs/etc/systemd/system/multi-user.target.wants/systemd-timesyncd.service

if [ -n "${rtc}" ]; then
cat > /rootfs/etc/systemd/system/hwclock-to-sysclock.service << EOF
[Unit]
Description=Set system clock from hardware clock
After=systemd-modules-load.service
[Service]
Type=oneshot
ExecStart=/sbin/hwclock --hctosys --utc
[Install]
WantedBy=basic.target
EOF
mkdir /rootfs/etc/systemd/system/basic.target.wants
ln -s /etc/systemd/system/hwclock-to-sysclock.service /rootfs/etc/systemd/system/basic.target.wants/hwclock-to-sysclock.service
fi
fi

# copy apt's sources.list to the target system
Expand Down Expand Up @@ -2248,7 +2279,7 @@ if [ "${i2c_enable}" = "1" ]; then
sed -i "s/^\(dtparam=i2c_arm=.*\)/#\1/" /rootfs/boot/config.txt
echo "dtparam=i2c_arm=on" >> /rootfs/boot/config.txt
fi
echo "i2c-dev" >> /rootfs/etc/modules
module_enable "i2c-dev" "i2c"
if [ -n "${i2c_baudrate}" ]; then
if grep -q "i2c_baudrate=" /rootfs/boot/config.txt; then
sed -i "s/\(.*i2c_baudrate=.*\)/#\1/" /rootfs/boot/config.txt
Expand Down Expand Up @@ -2379,11 +2410,8 @@ fi

# enable rtc if specified in the configuration file
if [ -n "${rtc}" ]; then
sed -i "s/^#\(dtoverlay=i2c-rtc,${rtc}\)/\1/" /rootfs/boot/config.txt
if [ "$(grep -c "^dtoverlay=i2c-rtc,.*" /rootfs/boot/config.txt)" -ne 1 ]; then
sed -i "s/^\(dtoverlay=i2c-rtc,\)/#\1/" /rootfs/boot/config.txt
echo "dtoverlay=i2c-rtc,${rtc}" >> /rootfs/boot/config.txt
fi
dtoverlay_enable "/rootfs/boot/config.txt" "i2c-rtc,${rtc}"
module_enable "rtc-${rtc}" "rtc"
fi

# enable custom dtoverlays
Expand Down Expand Up @@ -2456,7 +2484,7 @@ if echo "${cdebootstrap_cmdline} ${packages_postinstall}" | grep -q "fake-hwcloc
echo "OK"
elif [ -n "${rtc}" ]; then
echo -n "Saving current time to RTC... "
/opt/busybox/bin/hwclock --systohc || fail
/opt/busybox/bin/hwclock --systohc --utc || fail
echo "OK"
fi

Expand Down

0 comments on commit caf7423

Please sign in to comment.