Skip to content

Commit

Permalink
Merge branch 'master' into 8_digits
Browse files Browse the repository at this point in the history
  • Loading branch information
itzandroidtab committed Mar 7, 2024
2 parents 0d9d36b + 2674591 commit 6547ea5
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 24 deletions.
15 changes: 15 additions & 0 deletions linkerscript.ld
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Memories definitions
MEMORY
{
rom (rx) : org = 0x00000000 + 8k, len = 256k - 8k - 32k
/* Note: The profiles need to be sector alligned. The
storage erases the whole sector and writes the entries
back if we changed something. All other data in this
sector will be lost on changing the entries */
profiles (r) : org = 0x00000000 + 256k - 32k, len = 32k
ram (rwx) : org = 0x10000000, len = 16k
ram1 (rwx) : org = 0x2007C000, len = 16k
Expand Down Expand Up @@ -112,6 +116,17 @@ SECTIONS
PROVIDE(__fini_array_end = .);
} > rom

.profile (NOLOAD) :
{
/* provide the start and the end of the profile section */
PROVIDE(__profiles_start = .);

/* mark the whole section as filled with data */
. = . + LENGTH(profiles);

PROVIDE(__profiles_end = .);
} > profiles

/* Stack segment */
.stack (NOLOAD) :
{
Expand Down
7 changes: 1 addition & 6 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,8 @@ int main() {
// using for writing to flash
using flash = target::io::flash;

// the range where we store the keys is 0x38000 - 0x40000
// TODO: use the region from the linkerscript
constexpr static uint32_t key_start = 0x38000;
constexpr static uint32_t key_end = 0x40000;

// using for the storage
using storage = storage::storage<flash, key_start, key_end>;
using storage = storage::storage<flash>;

// create the popup first as some other screens use it
menu::numeric_popup<fb_t> numeric_popup = {};
Expand Down
1 change: 0 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ More pictures can be found [here](./img/)
* support for different intervals
* support for 8 digit keys
* add support for more than 32 profiles (needs a rework if more profiles are required. The profiles are copied to ram and there is not enough for more at the moment. If we leave them in flash we can store a lot more)
* support for reading the profile locations from the linkerscript instead of hardcoded
* rework the calibration and time settings screens

### Compiling
Expand Down
35 changes: 29 additions & 6 deletions storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
#include <klib/dynamic_array.hpp>
#include <klib/string.hpp>

extern "C" {
// Start of the profile section. Definition is done in the
// linkerscript. Only the address of the variable should be used. The
// address points to the correct location of the variable
extern uint32_t __profiles_start;

// End of the profile section. Definition is done in the
// linkerscript. Only the address of the variable should be used. The
// address points to the correct location of the variable
extern uint32_t __profiles_end;
}

namespace storage {
enum class digit: uint8_t {
digits_6 = 6,
Expand Down Expand Up @@ -53,7 +65,7 @@ namespace storage {
* @brief Storage class that reads the entries
*
*/
template <typename Flash, uint32_t Start, uint32_t End>
template <typename Flash>
class storage {
public:
// the max amount of entries we support for now
Expand All @@ -63,6 +75,9 @@ namespace storage {
// array to store all the entries
static inline klib::dynamic_array<entry, max_entries> entries = {};

// start address used in writing
static inline uint32_t start_address = 0xffffffff;

public:
/**
* @brief Read the memory and add them to the entries array
Expand All @@ -71,11 +86,13 @@ namespace storage {
* @param end
* @param key
*/
static void init(const std::span<uint8_t> key) {
uint32_t address = Start;
static void init(const std::span<uint8_t> key, const uint32_t start, const uint32_t end) {
// update the start address and set the address we should
// start reading from
uint32_t address = start_address = start;

// get all the entries
for (uint32_t i = 0; (i < max_entries) && ((address + sizeof(entry)) < End); i++) {
for (uint32_t i = 0; (i < max_entries) && ((address + sizeof(entry)) < end); i++) {
// TODO: decrypt the keys here
const auto& e = *reinterpret_cast<const entry*>(address);

Expand Down Expand Up @@ -110,8 +127,14 @@ namespace storage {
*
*/
static void write() {
// make sure we are initialized
if (start_address == 0xffffffff) {
// do not write if the start address is wrong
return;
}

// erase the sector
Flash::erase(Flash::erase_mode::sector, Start);
Flash::erase(Flash::erase_mode::sector, start_address);

// write in 1024 byte chunks
for (uint32_t i = 0; i < ((max_entries * sizeof(entry)) / 1024); i++) {
Expand All @@ -120,7 +143,7 @@ namespace storage {
};

// write the new data
Flash::write(Start + (i * 1024), p);
Flash::write(start_address + (i * 1024), p);
}
}
};
Expand Down
56 changes: 46 additions & 10 deletions ui/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <klib/filesystem/virtual_fat.hpp>
#include <klib/delay.hpp>
#include <klib/string.hpp>
#include <klib/crypt/base32.hpp>

#include "screen.hpp"

Expand Down Expand Up @@ -193,7 +194,8 @@ namespace menu {
"To create a new profile, add a new line below the CSV header in the following format:\r\n\r\n"
"profile name, interval, digits, key in hex or in string format.\r\n\r\nExample:\r\n"
"test, 30, 6, \"SOMEKEY123\"\r\ntest, 30, 6, 0xab 0xcd 0xef 0x12 0x34 0x56 0x78 0x90\r\n"
"test, 30, 6, abcdef1234567890\r\n\r\nprofile, interval, digits, key\r\n";
"test, 30, 6, abcdef1234567890\r\ntest, 30, 8, b32\"MFRGGZDFMYYTEMZUGU3DOOBZGA======\""
"\r\n\r\nprofile, interval, digits, key\r\n";

constexpr static char config_csv[] = "profile, interval, digits, key\r\n";

Expand Down Expand Up @@ -360,8 +362,8 @@ namespace menu {
}

// parse the key. The key is till the end of the line. We can either have
// a hex array or a string. Hex string needs to have "" around it. Search
// for the ""
// a hex array, string or a base32 string. Hex string needs to have "" around
// it. Search for the ""
const auto quotes = std::count(data, data + length, '"');

// check if we have a valid entry
Expand All @@ -370,18 +372,52 @@ namespace menu {
}

if (quotes) {
// we are in string mode
// check if have a base32 string or a hex string
const auto start = std::find(data, data + length, '"');
const auto end = std::find(start + 1, data + length, '"');

// copy the data directly into our buffer if it fits
if ((end - (start + 1)) >= entry.key.max_size()) {
return parse_result::invalid;
// check for base 32
if ((std::strncmp((start - 3), "b32", 3) == 0) ||
(std::strncmp((start - 3), "B32", 3) == 0))
{
// support up to 320 bits on the input and output
// input = 320 / 5 = 64 bytes
// output = 320 / 8 = 40 bytes
char input[65] = {};
uint8_t buf[40];

// check if we can copy it to the local buffer
if ((end - (start + 1)) >= (sizeof(input) - 1)) {
return parse_result::invalid;
}

// copy to the local buffer
std::copy_n((start + 1), end - (start + 1), input);

// decode the base32 value
const uint32_t size = klib::crypt::base32::decode(input, buf);

// check if the conversion was successfull
if (!size) {
return parse_result::invalid;
}

// copy the bytes to the key
for (uint32_t i = 0; i < size; i++) {
entry.key.push_back(buf[i]);
}
}
else {
// we are in string mode. copy the data directly
// into our buffer if it fits
if ((end - (start + 1)) >= entry.key.max_size()) {
return parse_result::invalid;
}

// add all the characters
for (uint32_t i = 0; i < (end - (start + 1)); i++) {
entry.key.push_back(start[1 + i]);
// add all the characters
for (uint32_t i = 0; i < (end - (start + 1)); i++) {
entry.key.push_back(start[1 + i]);
}
}

return parse_result::valid;
Expand Down
7 changes: 6 additions & 1 deletion ui/splash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <klib/graphics/bitmap.hpp>

#include <storage.hpp>

#include "screen.hpp"

namespace menu {
Expand Down Expand Up @@ -90,7 +92,10 @@ namespace menu {
Rtc::init();

// init the storage for all the keys
Storage::init({});
Storage::init({},
reinterpret_cast<uint32_t>(&__profiles_start),
reinterpret_cast<uint32_t>(&__profiles_end)
);

// init the usb driver + device
Usb::init();
Expand Down

0 comments on commit 6547ea5

Please sign in to comment.