Ben's notes

Linux, Unix, network, radio...

User Tools

Site Tools


initramfs_send_sms

Arch Linux - send SMS from initramfs

This is just a proof of concept. Might be useful to notify someone when a system has been booted and reached initramfs, and it is waiting for the decryption pass phrase.

Install packages

  • Install the required tools:
    pacman -S screen expect

Configure HSDPA module

Huawei MU609

  • The module might be configured for packet switched (PS) only. To be able to send SMS via AT commands, the module needs to be configured for CP_PS, CS or CS_ONLY, CS = circuit switched. If you also want to set up a dataconnection, configure CS_PS. If you don't include CS, you will receive the error: +CMS ERROR: 500 when sending an SMS.
    # screen /dev/ttyUSB0 115200
    at^syscfg=2,2,3FFFFFFF,1,2

Configure initramfs

  • Edit the mkinitcpio configuration file: /etc/mkinitcpio.conf:
    # Load these modules before any boot hooks are run
    MODULES="ehci_hcd ohci_pci usbserial option ohci_hcd ehci_pci"
    
    # Include these binaries
    BINARIES="/usr/bin/expect /usr/bin/stty"
    
    # Include the script and TCL dependency
    FILES="/usr/lib/tcl8.6/init.tcl /usr/local/bin/init-sms.sh"
    
    # Add custom 'sms' hook
    HOOKS="base udev autodetect modconf block keyboard sms encrypt filesystems fsck"

Create custom hook

  • /usr/lib/initcpio/hooks/sms:
    #!/usr/bin/ash
    
    run_hook() {
      /usr/local/bin/init-sms.sh 
    }
  • /usr/lib/initcpio/install/sms
    #!/bin/bash
    
    build() {
      add_runscript
    }

Create SMS script

  • This is just a simple quick expect script.
    #!/usr/bin/expect
    
    # device
    set modem /dev/ttyUSB0
    
    # keep it open
    exec sh -c "sleep 3 < $modem" &
    
    # serial port parameters (probably not needed for USB)
    exec stty -F $modem 9600 raw -clocal -echo -istrip -hup
    
    # connect
    spawn -open [open $modem w+]
    send "at\r"
    expect "OK"
    
    # Unlock SIM if needed.
    send "at^cpin?\r"
    expect { 
             # ^CPIN: SIM PIN,3,10,3,10,3
             "*CPIN: SIM PIN*" {
               send_user "SIM is locked, trying PIN...\n"
               send "at^cpin=123456\r"
               expect { 
                 "OK" {
                 }
                 "+CME ERROR: incorrect password" {
                   send_user "Wrong PIN, please configure the right PIN.\n"
                 }
               }
             }
    
             # ^CPIN: READY,,10,3,10,3
             "*CPIN: READY*" {
               send_user "SIM is ready.\n"
             }
    }
    
    # Wait 5 seconds, device is connecting to network...
    sleep 5
    
    # set text mode
    send "at+cmgf=1\r"
    expect "OK"
    
    # set GSM encoding
    send "at+csmp=,,,0\r"
    expect "OK"
    
    # Configure SMS service center
    send {AT+CSCA="+31653131313"}
    send "\r"
    expect "OK"
    
    # Select TE character set
    send {at+cscs="GSM"}
    send "\r"
    expect "OK"
    
    # Send SMS to number...
    send {at+cmgs="0612345678"}
    send "\r"
    expect "> "
    
    # Text to send, end with \x1A = ctrl-z
    send "System has reached initramfs."
    send "\x1a"

Generate new initramfs

  • Generate new initramfs:
    mkinitcpio -p linux
  • Reboot and test!
initramfs_send_sms.txt · Last modified: 2021/10/09 15:14 by 127.0.0.1