Lupino/machine

A toolbox for hack plc machine.

★ 0Forks 0HaskellGitHub ↗Compare

README

Machine

A toolbox for hack plc machine.

Commands

  • generator-addrmap Every plc machine has it's own addrmap, you must rewrite it for a new machine.
  • machine A monitor or remote control for plc machine. Or mock a real plc machine.
  • mock-lcd A tool for mock plc lcd.
  • generator-rsa-keypair Generate RSA public key and private key. Once generated you shoud copy to resources directory, replace machine.key and machine.key.pub.
  • rsa-encrypt RSA encrypt files use machine.key.pub.
  • rsa-decrypt RSA decrypt files use machine.key.
  • secure-machine The same as machine with encrypt config file and addrmap.

Installation

To build Machine from source you will need stack.

And make sure the location at which stack installs executables is in your PATH: export PATH=$HOME/.local/bin:$PATH.

stack build
stack install

Note: stack will need about 4GB of memory to compile successfully.

./bin/machine config.yml

Debian/Raspbian

Before building, you must install the "wiringPi" C library on your Raspberry Pi.

stack --stack-yaml stack-pi0.yaml install

# Make a release
./release/release.sh

tar xvf ./release/machine.tar.bz2 -C /usr/local

mv /usr/local/machine.service /etc/systemd/system

systemctl enable machine.service
systemctl start machine

Secure Machine

In the production envirement we use the secure-machine.

cp bin/secure-machine /usr/local/bin/machine
./bin/rsa-encrypt /usr/local/etc/machine/config.yml
mv /usr/local/etc/machine/config.yml.enc /usr/local/etc/machine/config.yml
./bin/rsa-encrypt /usr/local/etc/machine/addrmap
mv /usr/local/etc/machine/addrmap.enc /usr/local/etc/machine/addrmap
./bin/rsa-encrypt /usr/local/etc/machine/addrmap-small
mv /usr/local/etc/machine/addrmap-small.enc /usr/local/etc/machine/addrmap-small

Custom rsa keypair

./resources/update-rsa-keypair.sh
stack clean
stack install

Development

Mock PLC machine

# file: plc.yml

machine:
  req: /dev/cu.wchusbserial1420
  protect: false # when machine is started and protect is true mqtt request will only on memory
  start_addr: 2048 # machine start stat memory address
  timeout: 5000000 # serial recv timeout microseconds (1/10^6 seconds).
  serial_alive: 10000000 # serial alive time microseconds (1/10^6 seconds)
  serial_restart: 20000000 # serial auto restart delay microseconds (1/10^6 seconds)

memory:
  file: resources/memory.yml # initial memory from dump file
  # enable_dump: false # enable dump memory to file
  # dump_delay: 1 # seconds
  addrmap: resources/addrmap-small # generator by generator-addrmap-exe

log: plc.log # log to file
level: ERROR # log level DEBUG INFO NOTICE WARNING ERROR CRITICAL ALERT EMERGENCY
./bin/machine plc.yml

Mock PLC lcd

# file: lcd.yml

machine:
  res: /dev/cu.usbserial-FTDVITEJ
  protect: false # when machine is started and protect is true mqtt request will only on memory
  start_addr: 2048 # machine start stat memory address
  timeout: 5000000 # serial recv timeout microseconds (1/10^6 seconds).
  serial_alive: 10000000 # serial alive time microseconds (1/10^6 seconds)
  serial_restart: 20000000 # serial auto restart delay microseconds (1/10^6 seconds)

memory:
  # file: resources/memory.yml # initial memory from dump file
  # enable_dump: false # enable dump memory to file
  # dump_delay: 1 # seconds
  addrmap: resources/addrmap-small # generator by generator-addrmap-exe

log: lcd.log # log to file
level: ERROR # log level DEBUG INFO NOTICE WARNING ERROR CRITICAL ALERT EMERGENCY

mock:
  file: resources/reqs
  delay: 1000000 # command send every delay time microseconds (1/10^6 seconds)
  debug: false
./bin/mock-lcd lcd.yml

Cross Compilation

https://medium.com/@zw3rk/making-a-raspbian-cross-compilation-sdk-830fe56d75ba

The SDK

mkdir -p ${HOME}/raspbian-sdk/{prebuilt,sysroot}
wget http://releases.llvm.org/6.0.0/clang+llvm-6.0.0-x86_64-apple-darwin.tar.xz
tar xvf clang+llvm-6.0.0-x86_64-apple-darwin.tar.xz -C ${HOME}/raspbian-sdk/prebuilt --strip-components=1

wget https://ftp.gnu.org/gnu/binutils/binutils-2.28.tar.bz2

tar xvf binutils-2.28.tar.bz2
cd binutils-2.28
./configure --prefix="${HOME}/raspbian-sdk/prebuilt" \
            --target=arm-linux-gnueabihf \
            --enable-gold=yes \
            --enable-ld=yes \
            --enable-targets=arm-linux-gnueabihf \
            --enable-multilib \
            --enable-interwork \
            --disable-werror \
            --quiet
make
make install

rsync -rzLR --safe-links pi@raspberrypi:/usr/lib/arm-linux-gnueabihf ${HOME}/raspbian-sdk/sysroot
rsync -rzLR --safe-links pi@raspberrypi:/usr/lib/gcc/arm-linux-gnueabihf ${HOME}/raspbian-sdk/sysroot
rsync -rzLR --safe-links pi@raspberrypi:/usr/include ${HOME}/raspbian-sdk/sysroot
rsync -rzLR --safe-links pi@raspberrypi:/lib/arm-linux-gnueabihf ${HOME}/raspbian-sdk/sysroot

git clone https://github.com/zw3rk/toolchain-wrapper.git ${HOME}/raspbian-sdk/toolchain-wrapper

cd ${HOME}/raspbian-sdk/toolchain-wrapper

./bootstrap

Update config

# ${HOME}/raspbian-sdk/toolchain-wrapper/raspberrypi-toolchain.config
# Raspberry Pi
RPI_TARGET=arm-linux-gnueabihf
RPI_NDK=$HOME/raspbian-sdk
RPI_PATH=${RPI_NDK}/prebuilt/${RPI_TARGET}/bin/
RPI_SYSROOT=${RPI_NDK}/sysroot
RPI_TOOLCHAIN_LIB=${RPI_NDK}/sysroot/usr/lib/gcc/${RPI_TARGET}/6.3.0/

Direnv

# .envrc
export PATH=$HOME/raspbian-sdk/prebuilt/bin:$HOME/raspbian-sdk/toolchain-wrapper:$PATH

The cross ghc

make sure the host system is install ghc 8.6.4

wget https://downloads.haskell.org/~ghc/8.6.4/ghc-8.6.4-src.tar.xz

tar cjvf ghc-8.6.4-src.tar.xz

cd ghc-8.6.4

./configure --prefix=$HOME/raspbian-sdk/prebuilt \
            --target=arm-linux-gnueabihf \
            CC=arm-linux-gnueabihf-clang \
            LD=arm-linux-gnueabihf-ld.gold \
            NM=arm-linux-gnueabihf-nm \
            RANLIB=arm-linux-gnueabihf-ranlib \
            AS=arm-linux-gnueabihf-as \
            STRIP=arm-linux-gnueabihf-strip

config mk/build.mk

# mk/build.mk
BuildFlavour = quick-cross

build and install

make
make install

install denpends

some package can't cross compiling or failed, we shoud modify the cabal file and compile it.

entropy

git clone https://github.com/TomMD/entropy.git
cd entropy
git apply the-blow-patch
arm-linux-gnueabihf-cabal install --allow-newer

patch file

diff --git a/entropy.cabal b/entropy.cabal
index 6dd6af5..ff40c92 100644
--- a/entropy.cabal
+++ b/entropy.cabal
@@ -15,9 +15,9 @@ homepage:       https://github.com/TomMD/entropy
 bug-reports:    https://github.com/TomMD/entropy/issues
 stability:      stable

--- build-type:  Simple
+build-type:  Simple
 -- ^^ Used for HaLVM
-build-type:     Custom
+-- build-type:     Custom

 -- ^^ Test for RDRAND support using 'ghc'
 cabal-version:  >=1.10

cryptonite

git clone https://github.com/haskell-crypto/cryptonite.git
cd cryptonite
git apply the-blow-patch
arm-linux-gnueabihf-cabal install --allow-newer

patch file

diff --git a/cryptonite.cabal b/cryptonite.cabal
index 22f16c4..fca002a 100644
--- a/cryptonite.cabal
+++ b/cryptonite.cabal
@@ -84,7 +84,7 @@ Flag support_sse

 Flag integer-gmp
   Description:       Whether or not to use GMP for some functions
-  Default:           True
+  Default:           False
   Manual:            True

 Flag support_deepseq

clock

git clone https://github.com/corsis/clock.git
cd clock
arm-linux-gnueabihf-cabal install --allow-newer

wiringPi

git clone https://github.com/Lupino/hs-wiringPi.git
cd hs-wiringPi
arm-linux-gnueabihf-cabal install --allow-newer

modbus

git clone https://github.com/Lupino/modbus.git
cd modbus
arm-linux-gnueabihf-cabal install --allow-newer

compilation

arm-linux-gnueabihf-cabal install --allow-newer --bindir bin/arm-linux-gnueabihf

Contributors

Lupino

Issues