2025-08-02 18:05:56 +01:00

257 lines
8.2 KiB
C

#include <tap.h>
#include "../src/librnode.h"
#include "../src/libs/logging/log.h"
#include "../src/libs/util.h"
#include <time.h>
#include <openssl/evp.h>
#include <openssl/decoder.h>
#include <openssl/rsa.h>
#include <fcntl.h>
#include <string.h> // todo remove me
#include <openssl/sha.h> // todo remove me
//char* port = "/dev/serial/by-id/usb-Silicon_Labs_CP2102_USB_to_UART_Bridge_Controller_0001-if00-port0";
char* port = "/dev/ttyACM0";
//char* port = "/dev/serial/by-id/usb-Heltec_HT-n5262_1E35113239EFFE55-if00";
char* zip = "/home/elimin8/Downloads/rnode_firmware_t3s3_sx1280_pa.zip";
char* key = "/home/jake/.config/rnodeconf/firmware/signing.key";
uint32_t baud = 115200;
struct RNode rn = {0};
EVP_PKEY* load_key(char* path) {
EVP_PKEY *priv_key = EVP_PKEY_new();
OSSL_DECODER_CTX *dctx;
const char *format = "DER"; /* NULL for any format */
const char *structure = NULL; /* any structure */
const char *keytype = "RSA"; /* NULL for any key */
size_t len;
uint8_t key_buf[1024];
const uint8_t *data = key_buf;
FILE *file = fopen(path, "rb");
if (file <= 0) {
return NULL;
}
fseek(file, 0L, SEEK_END);
len = ftell(file);
fseek(file, 0L, SEEK_SET);
fread(key_buf, sizeof(uint8_t), len, file);
fclose(file);
dctx = OSSL_DECODER_CTX_new_for_pkey(&priv_key, format, structure,
keytype,
OSSL_KEYMGMT_SELECT_KEYPAIR,
NULL, NULL);
if (dctx == NULL) {
/* error: no suitable potential decoders found */
return NULL;
}
//if (pass != NULL)
// OSSL_DECODER_CTX_set_passphrase(dctx, pass, strlen(pass));
if (!OSSL_DECODER_from_data(dctx, &data, &len)) {
/* decoding failure */
return NULL;
}
OSSL_DECODER_CTX_free(dctx);
return priv_key;
}
void esp32_flash(struct RNode* rn, char* zip) {
uint8_t serial[4] = {0};
ok(rnode_set_platform(rn, PLATFORM_ESP32) == 0, "RNode set platform");
time_t start = time(NULL);
EVP_PKEY *priv_key = load_key(key);
ok(priv_key != NULL, "Load EEPROM signing key");
rn->product = 0xf0;
rn->model = 0xac;
rn->hw_rev = 0x01;
rn->boot_app0_addr = 0xe000;
rn->bootloader_addr = 0x0;
rn->bin_addr = 0x10000;
rn->partitions_addr = 0x8000;
rn->console_image_addr = 0x210000;
ok(rnode_flash(rn, zip, false, serial, priv_key, true) == 0, "RNode update");
time_t end = time(NULL);
printf("Time taken for flash: %ld seconds.\n", end - start);
}
void nrf52_flash(struct RNode* rn, char* zip) {
uint8_t serial[4] = {0};
ok(rnode_set_platform(rn, PLATFORM_NRF52) == 0, "RNode set platform");
time_t start = time(NULL);
EVP_PKEY *priv_key = load_key(key);
ok(priv_key != NULL, "Load EEPROM signing key");
rn->product = 0x10;
rn->model = 0x12;
rn->hw_rev = 0x01;
ok(rnode_flash(rn, zip, false, serial, priv_key, true) == 0, "RNode update");
time_t end = time(NULL);
printf("Time taken for flash: %ld seconds.\n", end - start);
}
int main() {
ok(rnode_init(&rn, port, baud, true, false) == 0, "RNode initialisation & detection");
//printf("--- RNode reset test ---\n\n");
//ok(rnode_reset(&rn) == 0, "RNode reset");
//sleep_ms(5000);
//ok(rnode_cleanup(&rn) == 0, "RNode cleanup");
//ok(rnode_init(&rn, port, baud, true, false) == 0, "RNode initialisation & detection");
//printf("--- RNode display test --- \n\n");
//ok(rnode_set_disp_int(&rn, 255) == 0, "Set display intensity to 255");
//sleep_ms(500);
//ok(rnode_set_disp_int(&rn, 0) == 0, "Set display intensity to 0");
//sleep_ms(500);
//ok(rnode_set_disp_int(&rn, 127) == 0, "Set display intensity to 127");
//sleep_ms(100);
//ok(rnode_set_disp_timeout(&rn, 1) == 0, "Set display timeout to 1 second");
//sleep_ms(5000);
//ok(rnode_set_disp_timeout(&rn, 1) == 0, "Disable display timeout");
//sleep_ms(500);
//ok(rnode_set_disp_addr(&rn, 0x3B) == 0, "Set display address to 0x3B");
//sleep_ms(500);
//ok(rnode_set_disp_addr(&rn, 0x3C) == 0, "Set display address to 0x3C");
//sleep_ms(500);
//ok(rnode_set_disp_rot(&rn, 0) == 0, "Set display rotation to 0");
//sleep_ms(2000);
//ok(rnode_cleanup(&rn) == 0, "RNode cleanup");
//ok(rnode_init(&rn, port, baud, true, false) == 0, "RNode initialisation & detection");
//ok(rnode_set_disp_rot(&rn, 1) == 0, "Set display rotation to 1");
//sleep_ms(2000);
//ok(rnode_cleanup(&rn) == 0, "RNode cleanup");
//ok(rnode_init(&rn, port, baud, true, false) == 0, "RNode initialisation & detection");
//ok(rnode_set_disp_rot(&rn, 2) == 0, "Set display rotation to 2");
//sleep_ms(2000);
//ok(rnode_cleanup(&rn) == 0, "RNode cleanup");
//ok(rnode_init(&rn, port, baud, true, false) == 0, "RNode initialisation & detection");
//ok(rnode_set_disp_rot(&rn, 3) == 0, "Set display rotation to 3");
//sleep_ms(2000);
//ok(rnode_cleanup(&rn) == 0, "RNode cleanup");
//ok(rnode_init(&rn, port, baud, true, false) == 0, "RNode initialisation & detection");
//ok(rnode_start_disp_recon(&rn) == 0, "Start display reconditioning (5s)");
//sleep_ms(5000);
//ok(rnode_reset(&rn) == 0, "RNode reset");
//sleep_ms(5000);
//ok(rnode_cleanup(&rn) == 0, "RNode cleanup");
//ok(rnode_init(&rn, port, baud, true, false) == 0, "RNode initialisation & detection");
//printf("--- RNode NP test (if present) --- \n\n");
//ok(rnode_set_np_int(&rn, 0) == 0, "Set NP intensity to 0");
//sleep_ms(500);
//ok(rnode_set_np_int(&rn, 255) == 0, "Set NP intensity to 255");
//sleep_ms(500);
//
//printf("--- RNode BT test --- \n\n");
//ok(rnode_set_bt(&rn, BT_OFF) == 0, "Disable Bluetooth");
//sleep_ms(1000);
//ok(rnode_set_bt(&rn, BT_ON) == 0, "Enable Bluetooth");
//sleep_ms(1000);
//ok(rnode_set_bt(&rn, BT_PAIRING) == 0, "Enable Bluetooth pairing");
//sleep_ms(1000);
//int pin = rnode_get_bt_pin(&rn, 30000);
//ok(pin > 0, "Get Bluetooth code (30s timeout)");
//printf("Pin: %u\n", pin);
//ok(rnode_wipe_eeprom(&rn) == 0, "Wipe RNode EEPROM");
//esp32_flash(&rn, zip);
//printf("--- RNode flash & provision test ---\n\n");
//printf("--- RNode TNC mode test ---\n\n");
//ok(rnode_get_interfaces(&rn) == 0, "Get interfaces");
//ok(rnode_select_interface(&rn, 0) == 0, "Select interface 0");
//ok(rnode_set_freq(&rn, 865700000) > 0, "Set frequency");
//ok(rnode_set_sf(&rn, 7) == 0, "Set spreading factor");
//ok(rnode_set_cr(&rn, 7) == 0, "Set coding rate");
//ok(rnode_set_txp(&rn, 7) == 0, "Set TX power");
//ok(rnode_set_st_alock(&rn, 10) == 0, "Set ST alock");
//ok(rnode_set_lt_alock(&rn, 10) == 0, "Set LT alock");
//ok(rnode_set_interface_state(&rn, true) == 0, "Enable radio interface");
//ok(rnode_set_tnc_mode(&rn) == 0, "Enable TNC mode");
//printf("--- RNode EEPROM read test ---\n\n");
//ok(rnode_get_eeprom(&rn) == 0, "Retrieve EEPROM");
printf("--- RNode EEPROM provision test ---\n\n");
ok(rnode_set_product(&rn, 0x20) == 0, "Set EEPROM product");
ok(rnode_set_model(&rn, 0x21) == 0, "Set EEPROM product");
ok(rnode_set_hw_rev(&rn, 0x01) == 0, "Set hardware revision");
uint8_t serial[4] = {0};
ok(rnode_set_serial(&rn, serial) == 0, "Set serial");
ok(rnode_set_made_time(&rn, 3200) == 0, "Set manufacture time & date");
uint8_t checksum[16] = {0};
ok(rnode_calculate_checksum(&rn, checksum) == 0, "Calculate checksum");
ok(rnode_set_checksum(&rn, checksum) == 0, "Set checksum");
EVP_PKEY *priv_key = load_key(key);
void* signature = rnode_generate_signature(&rn, priv_key);
ok(signature != NULL, "Generate signature");
ok(rnode_set_signature(&rn, signature) == 0, "Set signature");
OPENSSL_free(signature);
ok(rnode_set_lock(&rn) == 0, "Set EEPROM lock");
uint8_t hash[32];
memcpy(hash, (uint8_t[32]){0x56, 0x92, 0x82, 0xa8, 0x3a, 0xbf, 0x21, 0xa5, 0x9c, 0x74, 0x9c, 0x7f, 0x96, 0xe0, 0x09, 0x12, 0x8d, 0xbf, 0x13, 0x9b, 0x99, 0xd2, 0x9c, 0x0b, 0x6f, 0x13, 0xd0, 0xb0, 0x4e, 0xe8, 0x6d, 0xac}, 32*sizeof(uint8_t));
ok(rnode_set_fw_hash(&rn, hash) == 0, "Set firmware hash");
ok(rnode_cleanup(&rn) == 0, "RNode cleanup");
done_testing();
}