tillitis / tkey-libs

TKey device libs
GNU General Public License v2.0
4 stars 2 forks source link

Can't properly load (or compile?) a device application #24

Closed LoupVaillant closed 1 year ago

LoupVaillant commented 1 year ago

So by now I have done everything I could, I'm pretty sure the error is not mine: I have a reliable way to find the TKey talk to its firmware so it gives me its name & version, I can load a device application and it answers with the hash of that application. I computed the same hash (using the reference BLAKE2s implementation from the RFC) locally, they compare equal, so I know the TKey sees what I see. I'm also pretty sure I'm hashing and sending the correct data (that is, the entire file): the hash is stable and Valgrind doesn't complain.

And yet, my TKeys goes blinking red instead of going christmas tree. Here's the device program I'm trying to load:

#include <stdint.h>
#include <tkey/led.h>

#define SLEEPTIME 100000

void sleep(uint32_t n)
{
    for (volatile int i = 0; i < n; i++);
}

int main(void)
{
    for (;;) {
//      set_led(LED_RED);    sleep(SLEEPTIME);
        set_led(LED_GREEN);  sleep(SLEEPTIME);
        set_led(LED_BLUE);   sleep(SLEEPTIME);
    }
    return 0;
}

Here's the command I used to compile it:

clang  -target riscv32-unknown-none-elf  -march=rv32iczmmul  -mabi=ilp32  -mcmodel=medany  -static  -std=gnu99  -fno-common  -fno-builtin-printf  -fno-builtin-putchar  -nostdlib  -mno-relax   -O2 -ffast-math -flto  -Wall -Werror=implicit-function-declaration  -I tkey-libs/include   -c led.c -o led.o
clang  -target riscv32-unknown-none-elf  -march=rv32iczmmul  -mabi=ilp32  -mcmodel=medany  -static  -std=gnu99  -fno-common  -fno-builtin-printf  -fno-builtin-putchar  -nostdlib  -mno-relax   -O2 -ffast-math -flto  -Wall -Werror=implicit-function-declaration  -I tkey-libs/include   -T tkey-libs/app.lds  -L tkey-libs -lcrt0 -lcommon -lmonocypher   led.o -o led.elf

My system and clang version:

$ uname -a
Linux black-pad 6.2.0-33-generic #33-Ubuntu SMP PREEMPT_DYNAMIC Tue Sep  5 14:49:19 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
$ clang --version
Ubuntu clang version 15.0.7
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

Also, please find attached my compiled device application (compressed tgz because GitHub).

Can you find anything I did wrong?

dehanj commented 1 year ago

Hi,

Sorry for the slow answer, for some reason we missed the issue..

Anyhow, I copied your exact code and pasted over the blue.c in tkey-libs/example-app. Just hit make, after I had build tkey-libs for tag v0.0.2. I used tkey-runapp and my TKey goes green and blue. So it is nothing wrong with your C-code. I tried both with clang 15 and 16, both works fine. You can find my flags to the compiler in the tkey-libs/example-app/Makefile.

I see that you attach an .elf file, the Tkey requires a raw binary. So I ran llvm-objcopy --input-target=elf32-liitleriscv --output-target=binary led.elf led.bin to create bin-file from your attached elf-file and loaded it to my TKey with tkey-runapp. It works, got a green and blue LED.

What happens if you convert it to a binary and load it? You can also try with tkey-runapp.

LoupVaillant commented 1 year ago

Damn. For some reason I missed the call to llvm-objcopy. I just ran it as you said, and got a working blinking led (green/blue), thanks!

dehanj commented 12 months ago

Glad it helped!