Skip to content

Commit

Permalink
Persisted passcodes to onboard flash (cf. #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed Sep 28, 2023
1 parent 41578db commit dea5d08
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 46 deletions.
9 changes: 9 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
- [x] unlock on valid passcode
- [x] master passcode
- [ ] set passcodes
- [ ] include passcodes in CRC
- (?) clear ACL
- [ ] Weird card after ACL grant/revoke 10058400:
```
> ACL 10058399
> ACL 2147483647
> ACL 10058444
```

- [ ] 8-bit burst mode write
- [ ] 8-bit burst mode read
- [ ] card + PIN writer
Expand Down
4 changes: 2 additions & 2 deletions pico/core/include/flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

#include "wiegand.h"

extern void flash_read_acl(CARD[], int *);
extern void flash_write_acl(CARD[], int);
extern void flash_read_acl(CARD[], int *, uint32_t[4]);
extern void flash_write_acl(CARD[], int, uint32_t[4]);
15 changes: 13 additions & 2 deletions pico/core/src/acl.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ int acl_load() {
int N = ACL_SIZE;
char s[64];

flash_read_acl(ACL, &N);
flash_read_acl(ACL, &N, PASSCODES);
snprintf(s, sizeof(s), "ACL LOADED %d CARDS FROM FLASH", N);
logd_log(s);

Expand Down Expand Up @@ -75,14 +75,20 @@ int acl_save() {
int N = 0;
char s[64];

for (int i = 0; i < ACL_SIZE; i++) {
if (ACL[i].card_number > 99965535) {
ACL[i].card_number = 0xffffffff;
}
}

for (int i = 0; i < ACL_SIZE; i++) {
if (ACL[i].card_number != 0xffffffff) {
N++;
}
}

// ... save to flash
flash_write_acl(ACL, ACL_SIZE);
flash_write_acl(ACL, ACL_SIZE, PASSCODES);
snprintf(s, sizeof(s), "ACL STORED %d CARDS TO FLASH", N);
logd_log(s);

Expand Down Expand Up @@ -215,11 +221,16 @@ bool acl_allowed(uint32_t facility_code, uint32_t card) {
*
*/
bool acl_set_passcodes(uint32_t passcode1, uint32_t passcode2, uint32_t passcode3, uint32_t passcode4) {
// ... set in-memory passcodes
PASSCODES[0] = passcode1 > 0 && passcode1 < 1000000 ? passcode1 : 0;
PASSCODES[1] = passcode2 > 0 && passcode2 < 1000000 ? passcode2 : 0;
PASSCODES[2] = passcode3 > 0 && passcode3 < 1000000 ? passcode3 : 0;
PASSCODES[3] = passcode4 > 0 && passcode4 < 1000000 ? passcode4 : 0;

// ... save to flash
flash_write_acl(ACL, ACL_SIZE, PASSCODES);
logd_log("ACL UPDATED PASSCODES IN FLASH");

return true;
}

Expand Down
54 changes: 25 additions & 29 deletions pico/core/src/flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

const uint32_t OFFSETS[2] = {
PICO_FLASH_SIZE_BYTES - FLASH_SECTOR_SIZE,
PICO_FLASH_SIZE_BYTES - 2 * FLASH_SECTOR_SIZE};
PICO_FLASH_SIZE_BYTES - 2 * FLASH_SECTOR_SIZE,
};

const int PAGES = sizeof(OFFSETS) / sizeof(uint32_t);

Expand All @@ -36,15 +37,25 @@ uint32_t crc32(const char *, size_t);
/* Reads the ACL from onboard flash.
*
*/
void flash_read_acl(CARD cards[], int *N) {
void flash_read_acl(CARD cards[], int *N, uint32_t passcodes[4]) {
uint32_t page = flash_get_current_page();

if (page != -1 && page < PAGES) {
uint32_t addr = XIP_BASE + OFFSETS[page];
uint32_t size = *(((uint32_t *)addr) + 2);
uint32_t *p = (uint32_t *)(addr + HEADER_SIZE);
uint32_t *q = (uint32_t *)(addr) + 32;
int ix = 0;

uint32_t *r = (uint32_t *)(addr);

// ... get passcodes
passcodes[0] = *q++;
passcodes[1] = *q++;
passcodes[2] = *q++;
passcodes[3] = *q++;

// ... get cards
for (uint32_t i = 0; i < size && ix < *N; i++) {
uint32_t card = *(p + 0);
uint32_t start = *(p + 1);
Expand All @@ -60,26 +71,11 @@ void flash_read_acl(CARD cards[], int *N) {
cards[ix].allowed = allowed;
snprintf(cards[ix].name, CARD_NAME_SIZE, name);

// char s[128];
//
// snprintf(s, sizeof(s), ">>>> CARD %-2lu %-8lu %04d-%02d-%02d %04d-%02d-%02d %s %s",
// i + 1,
// cards[ix].card_number,
// cards[ix].start.year,
// cards[ix].start.month,
// cards[ix].start.day,
// cards[ix].end.year,
// cards[ix].end.month,
// cards[ix].end.day,
// cards[ix].allowed ? "Y" : "N",
// cards[ix].name);
//
// logd_debug(s);

ix++;
}

*N = ix;

return;
}

Expand All @@ -91,7 +87,7 @@ void flash_read_acl(CARD cards[], int *N) {
/* Writes the ACL to onboard flash.
*
*/
void flash_write_acl(CARD cards[], int N) {
void flash_write_acl(CARD cards[], int N, uint32_t passcodes[4]) {
uint32_t page = flash_get_current_page();
uint32_t version = flash_get_version(page);

Expand All @@ -107,6 +103,14 @@ void flash_write_acl(CARD cards[], int N) {
uint32_t buffer[FLASH_SECTOR_SIZE / sizeof(uint32_t)];
struct header header;

memset(buffer, 0, sizeof(buffer));

// ... set passcodes
buffer[32] = passcodes[0];
buffer[32 + 1] = passcodes[1];
buffer[32 + 2] = passcodes[2];
buffer[32 + 3] = passcodes[3];

// .. fill cards buffer
uint32_t *p = (uint32_t *)(&buffer[64]);
int ix = 0;
Expand Down Expand Up @@ -143,8 +147,9 @@ void flash_write_acl(CARD cards[], int N) {
buffer[2] = header.cards;
buffer[3] = header.crc;

// // ... write to flash
// ... write to flash
uint32_t interrupts = save_and_disable_interrupts();

flash_range_erase(offset, FLASH_SECTOR_SIZE);
flash_range_program(offset, (uint8_t *)buffer, FLASH_SECTOR_SIZE);
restore_interrupts(interrupts);
Expand All @@ -165,15 +170,6 @@ int flash_get_current_page() {
header.cards = *p++;
header.crc = *p++;

// char s[128];
//
// snprintf(s, sizeof(s), ">>>> HEADER magic:%08X version:%lu cards:%lu crc:%08x",
// header.magic,
// header.version,
// header.cards,
// header.crc);
// logd_debug(s);

if (header.magic == ACL_MAGIC_WORD && header.version < ACL_VERSION && header.cards <= 60) {
uint32_t crc = crc32((char *)(addr + HEADER_SIZE), header.cards * 64);

Expand Down
13 changes: 0 additions & 13 deletions pico/core/src/sdcard.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,6 @@ int sdcard_read_acl(CARD cards[], int *N) {
}
}

// char s[128];
// snprintf(s, sizeof(s), ">>>> CARD %-8lu %04d-%02d-%02d %04d-%02d-%02d %s %s",
// cards[ix].card_number,
// cards[ix].start.year,
// cards[ix].start.month,
// cards[ix].start.day,
// cards[ix].end.year,
// cards[ix].end.month,
// cards[ix].end.day,
// cards[ix].allowed ? "Y" : "N",
// cards[ix].name);
// logd_debug(s);

ix++;
}
}
Expand Down

0 comments on commit dea5d08

Please sign in to comment.