udev + bossa = failure to autorecover?



  • Had another silly idea; if my RPi should detect a BOSSA device plugged in, why not have it automatically flash the board with the appropriate firmware.

    While all the Duets have different usb id's when running the firmware, while in the bootloader they're all the same - fortunately bossac can query the chip and from the chip family we can determine which firmware is correct.

    $ lsusb
    Bus 001 Device 006: ID 1d50:60ec OpenMoko, Inc. (Duet 2 Wifi)
    Bus 001 Device 006: ID 1d50:60ed OpenMoko, Inc. (Duet 2 Maestro)
    Bus 001 Device 004: ID 1d50:60ee OpenMoko, Inc. (Duet 3)
    Bus 001 Device 008: ID 03eb:6124 Atmel Corp. at91sam SAMBA bootloader
    

    So to get the ball rolling we need some udev rules.

    /etc/udev/rules/99-duet.rules

    ACTION=="add", ATTRS{idVendor}=="03eb", ATTRS{idProduct}=="6124", RUN+="/home/pi/duet_bossac.sh"
    
    

    and reload sudo udevadm control -R

    /home/pi/duet_bossac.sh contain:

    #!/bin/bash
    logger "$0 started"
    BOSSAC=~/BOSSA/bin/bossac
    if [ ! -f "$BOSSAC" ]; then
      [ ! -f /usr/bin/git ] && sudo apt install -y git
      git clone https://github.com/shumatech/BOSSA ~/BOSSA
      cd ~/BOSSA
      make bossac
    fi
    
    #RRF2 files and urls
    D2MF=/home/pi/DuetMaestroFirmware.bin
    D2MFURL=https://github.com/dc42/RepRapFirmware/releases/download/2.05.1/DuetMaestroFirmware.bin
    D2CF=Duet2CombinedFirmware.bin
    D2CFURL=https://github.com/dc42/RepRapFirmware/releases/download/2.05.1/Duet2CombinedFirmware.bin
    
    #RRF3 files and urls
    #D2MF=/home/pi/DuetMaestroFirmware.bin
    #D2MFURL=https://github.com/dc42/RepRapFirmware/releases/download/3.0/DuetMaestroFirmware.bin
    #D2CF=/home/pi/Duet2CombinedFirmware.bin
    #D2CFURL=https://github.com/dc42/RepRapFirmware/releases/download/3.0/Duet2CombinedFirmware.bin
    D3=/home/pi/Duet3Firmware_MB6HC.bin
    D3URL=https://github.com/dc42/RepRapFirmware/releases/download/3.0/Duet3Firmware_MB6HC.bin
    
    case `$BOSSAC -i | grep Device | cut -d : -f2 ` in
      " ATSAM4S8")
        logger "$0 Duet 2 Maestro"
        [ ! -f "$D2MF" ] && wget "$D2MFURL" -O $D2MF
        $BOSSAC -e -w -v -b $D2MF -R
        ;;
      " ATSAM4E8")
        logger "$0 Duet 2 Wifi/Ethernet"
        [ ! -f "$D2CF" ] && wget "$D2CFURL" -O $D2CF
        $BOSSAC -e -w -v -b $D2CF -R
        ;;
      " ATSAME70")
        logger "$0 Duet 3 6HC"
        [ ! -f "$D3" ] && wget "$D3URL" -O $D3
        $BOSSAC -e -w -v -b $D3 -R
        ;;
      *)
        echo CPU not identified.
        ;;
    esac
    

    Connecting a erased board shows udev is starting the script but it doesn't do much. I run the script as pi and it works fine as show by the logs

    Mar 16 05:40:12 duet3 kernel: [ 1980.232111] usb 1-1.1.2: USB disconnect, device number 16
    Mar 16 05:40:18 duet3 kernel: [ 1986.668315] usb 1-1.1.2: new full-speed USB device number 17 using dwc_otg
    Mar 16 05:40:19 duet3 kernel: [ 1986.800823] usb 1-1.1.2: New USB device found, idVendor=03eb, idProduct=6124, bcdDevice= 1.10
    Mar 16 05:40:19 duet3 kernel: [ 1986.800837] usb 1-1.1.2: New USB device strings: Mfr=0, Product=0, SerialNumber=0
    Mar 16 05:40:19 duet3 kernel: [ 1986.801842] cdc_acm 1-1.1.2:1.0: ttyACM0: USB ACM device
    Mar 16 05:40:19 duet3 root: /home/pi/duet_bossac.sh started
    Mar 16 05:40:19 duet3 root: /home/pi/duet_bossac.sh started
    Mar 16 05:40:19 duet3 root: /home/pi/duet_bossac.sh started
    Mar 16 05:40:19 duet3 root: /home/pi/duet_bossac.sh started
    Mar 16 05:40:27 duet3 pi: duet_bossac.sh started
    Mar 16 05:40:28 duet3 pi: duet_bossac.sh Duet 2 Maestro
    Mar 16 05:40:45 duet3 kernel: [ 2013.512104] usb 1-1.1.2: USB disconnect, device number 17
    Mar 16 05:40:46 duet3 kernel: [ 2013.808063] usb 1-1.1.2: new full-speed USB device number 18 using dwc_otg
    Mar 16 05:40:46 duet3 kernel: [ 2013.941183] usb 1-1.1.2: New USB device found, idVendor=1d50, idProduct=60ed, bcdDevice= 1.00
    Mar 16 05:40:46 duet3 kernel: [ 2013.941197] usb 1-1.1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=0
    Mar 16 05:40:46 duet3 kernel: [ 2013.941207] usb 1-1.1.2: Product: Duet
    Mar 16 05:40:46 duet3 kernel: [ 2013.941216] usb 1-1.1.2: Manufacturer: Duet3D
    Mar 16 05:40:46 duet3 kernel: [ 2013.942212] cdc_acm 1-1.1.2:1.0: ttyACM0: USB ACM device
    

    A little digging it seems udev runs in a sandbox that prevent access to networks and mounting file systems; while the script does download missing firmware files and tools it fails even if everything is in place.

    Maybe i just have to parse udevadm monitor or something to get around the sandbox, but if anyone has ideas I'm all ears!


Log in to reply