

# ファームウェア更新ファイルと署名を生成する
<a name="lorawan-script-fwupdate-sigkey"></a>

この手順のステップはオプションで、使用しているゲートウェイによって異なります。ゲートウェイの製造元は、独自のファームウェア更新を更新ファイルまたはスクリプトの形式で提供し、Basics Station はこのスクリプトをバックグラウンドで実行します。この場合、ほとんどの場合、使用しているゲートウェイのリリースノートにファームウェア更新ファイルが記載されています。その更新ファイルまたはスクリプトを代わりに使用して、「[S3 バケットにファームウェアファイルをアップロードし、IAM ロールを追加する](lorawan-upload-firmware-s3bucket.md)」に進むことができます。

このスクリプトがない場合は、以下に示す、ファームウェア更新ファイルを生成するために実行するコマンドを使用してください。また、更新に署名して、コードが変更および破損していないこと、信頼できる作成者のみが発行したコードがデバイスで実行されるようにすることもできます。

**Topics**
+ [ファームウェア更新ファイルを生成する](#lorawan-firmware-update-script)
+ [ファームウェア更新の署名を生成する](#lorawan-generate-signature-fwupdate)
+ [次のステップを確認する](#lorawan-fwupdate-sigkey-next-steps)

## ファームウェア更新ファイルを生成する
<a name="lorawan-firmware-update-script"></a>

ゲートウェイで動作している LoRa Basics Station ソフトウェアは、CUPS 応答でファームウェア更新を受信できます。製造元から提供されたスクリプトがない場合は、Raspberry Pi ベースの RAKWireless ゲートウェイ用に書かれた次のファームウェア更新スクリプトを参照してください。基本スクリプトと、新しいステーションバイナリ、バージョンファイル、`station.conf` が添付されています。

**注記**  
スクリプトは RAKWireless ゲートウェイに固有のものなので、使用しているゲートウェイに応じてアプリケーションに適応させる必要があります。

**基本スクリプト**  
以下は、Raspberry Pi ベースの RAKWireless ゲートウェイのサンプル基本スクリプトを示しています。次のコマンドを `base.sh` ファイルに保存し、端末で、Raspberry Pi のウェブブラウザからスクリプトを実行できます。

```
*#!/bin/bash*
execution_folder=/home/pi/Documents/basicstation/examples/aws_lorawan
station_path="$execution_folder/station"
version_path="$execution_folder/version.txt"
station_conf_path="$execution_folder/station_conf"

# Function to find the Basics Station binary at the end of this script 
# and store it in the station path
function prepare_station()
{
 match=$(grep --text --line-number '^STATION:$' $0 | cut -d ':' -f 1) 
 payload_start=$((match + 1)) 
 match_end=$(grep --text --line-number '^END_STATION:$' $0 | cut -d ':' -f 1) 
 payload_end=$((match_end - 1)) 
 lines=$(($payload_end-$payload_start+1)) 
 head -n $payload_end $0 | tail -n $lines  > $station_path
}

# Function to find the version.txt at the end of this script 
# and store it in the location for version.txt
function prepare_version()
{
  match=$(grep --text --line-number '^VERSION:$' $0 | cut -d ':' -f 1) 
  payload_start=$((match + 1))        
  match_end=$(grep --text --line-number '^END_VERSION:$' $0 | cut -d ':' -f 1) 
  payload_end=$((match_end - 1)) 
  lines=$(($payload_end-$payload_start+1)) 
  head -n $payload_end $0 | tail -n $lines  > $version_path
}

# Function to find the version.txt at the end of this script 
# and store it in the location for version.txt
function prepare_station_conf()
{
 match=$(grep --text --line-number '^CONF:$' $0 | cut -d ':' -f 1) 
 payload_start=$((match + 1)) 
 match_end=$(grep --text --line-number '^END_CONF:$' $0 | cut -d ':' -f 1) 
 payload_end=$((match_end - 1)) 
 lines=$(($payload_end-$payload_start+1)) 
 head -n $payload_end $0 | tail -n $lines  > $station_conf_path
}

# Stop the currently running Basics station so that it can be overwritten
# by the new one
killall station

# Store the different files
prepare_station
prepare_versionp
prepare_station_conf

# Provide execute permission for Basics station binary
chmod +x $station_path

# Remove update.bin so that it is not read again next time Basics station starts
rm -f /tmp/update.bin

# Exit so that rest of this script which has binaries attached does not get executed
exit 0
```

**ペイロードスクリプトを追加する**  
基本スクリプトに、Basics Station バイナリ、更新するバージョンを識別する version.txt、および `station.conf` を追加して、`addpayload.sh` というスクリプトにしました。次に、このスクリプトを実行します。

```
*#!/bin/bash
*
base.sh > fwstation

# Add station
echo "STATION:" >> fwstation
cat $1 >> fwstation
echo "" >> fwstation
echo "END_STATION:" >> fwstation

# Add version.txt
echo "VERSION:" >> fwstation
cat $2 >> fwstation
echo "" >> fwstation
echo "END_VERSION:" >> fwstation

# Add station.conf
echo "CONF:" >> fwstation
cat $3 >> fwstation
echo "END_CONF:" >> fwstation

# executable
chmod +x fwstation
```

これらのスクリプトを実行したら、端末で次のコマンドを実行して、ファームウェア更新ファイル `fwstation` を生成します。

```
$ ./addpayload.sh station version.txt station.conf
```

## ファームウェア更新の署名を生成する
<a name="lorawan-generate-signature-fwupdate"></a>

LoRa Basics Stationソフトウェアは、ECDSA 署名付きファームウェア更新を提供します。署名付き更新をサポートするには、以下が必要です。
+ ECDSA プライベートキーによって生成され、128 バイト未満である必要がある署名。
+ 署名に使用されるプライベートキー。`sig-%d.key` の形式のファイル名を持つゲートウェイに格納する必要があります。ファイル名 `sig-0.key` を使用することをお勧めします。
+ プライベートキーを介した 32 ビットの CRC。

署名と CRC は AWS IoT Core for LoRaWAN API に渡されます。前述のファイルを生成するには、次の `gen.sh` スクリプトを使用できます。これは、GitHub リポジトリの [basicstation](https://github.com/lorabasics/basicstation/blob/master/examples/cups/prep.sh) の例から着想を得たものです。

```
*#!/bin/bash

*function ecdsaKey() {
    # Key not password protected for simplicity    
    openssl ecparam -name prime256v1 -genkey | openssl ec -out $1
}

# Generate ECDSA key
ecdsaKey sig-0.prime256v1.pem

# Generate public key
openssl ec -in sig-0.prime256v1.pem -pubout -out sig-0.prime256v1.pub

# Generate signature private key
openssl ec -in sig-0.prime256v1.pub -inform PEM -outform DER -pubin | tail -c 64 > sig-0.key

# Generate signature
openssl dgst -sha512 -sign sig-0.prime256v1.pem $1 > sig-0.signature

# Convert signature to base64
openssl enc -base64 -in sig-0.signature -out sig-0.signature.base64

# Print the crc
crc_res=$(crc32 sig-0.key)printf "The crc for the private key=%d\n" $((16#$crc_res))

# Remove the generated files which won't be needed later
rm -rf sig-0.prime256v1.pem sig-0.signature sig-0.prime256v1.pub
```

スクリプトによって生成されたプライベートキーは、ゲートウェイに保存する必要があります。キーファイルはバイナリ形式です。

```
./gen_sig.sh fwstation 
read EC key
writing EC key
read EC key
writing EC key
read EC key
writing EC key
The crc for the private key=3434210794

$ cat sig-0.signature.base64 
MEQCIDPY/p2ssgXIPNCOgZr+NzeTLpX+WfBo5tYWbh5pQWN3AiBROen+XlIdMScv
AsfVfU/ZScJCalkVNZh4esyS8mNIgA==

$ ls sig-0.key
sig-0.key

$ scp sig-0.key pi@192.168.1.11:/home/pi/Documents/basicstation/examples/iotwireless
```

## 次のステップを確認する
<a name="lorawan-fwupdate-sigkey-next-steps"></a>

ファームウェアと署名の生成が完了したので、次のトピックに進み、ファームウェアファイル `fwstation` を Amazon S3 バケットにアップロードします。バケットは、ファームウェア更新ファイルをオブジェクトとして格納するコンテナです。S3 バケット内のファームウェア更新ファイルを読み取る許可を CUPS サーバーに付与する IAM ロールを追加できます。