Skip to content

Commit

Permalink
Included passcodes in flash CRC (cf. #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed Oct 2, 2023
1 parent dea5d08 commit d71447f
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 31 deletions.
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
- [x] unlock on valid passcode
- [x] master passcode
- [ ] set passcodes
- [ ] include passcodes in CRC
- [x] include passcodes in CRC
- (?) clear ACL
- [ ] Weird card after ACL grant/revoke 10058400:
- [ ] Weird card after ACL revoking a card in the middle of a list
```
> ACL 10058399
> ACL 2147483647
Expand Down
14 changes: 1 addition & 13 deletions pico/controller/common/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "acl.h"
#include "common.h"
#include "logd.h"
#include "relays.h"
#include "sdcard.h"
#include "sys.h"
#include "tcpd.h"
Expand All @@ -25,8 +24,6 @@ void query(txrx, void *);

void on_card_command(char *cmd, handler fn, txrx, void *);

void on_door_unlock(txrx, void *);

void grant(uint32_t, uint32_t, txrx, void *);
void revoke(uint32_t, uint32_t, txrx, void *);
void list_acl(txrx, void *);
Expand Down Expand Up @@ -78,7 +75,7 @@ void execw(char *cmd, txrx f, void *context) {
} else if (strncasecmp(cmd, "query", 5) == 0) {
query(f, context);
} else if (strncasecmp(cmd, "unlock", 6) == 0) {
on_door_unlock(f, context);
cli_unlock_door(f, context);
} else if (strncasecmp(cmd, "mount", 5) == 0) {
mount(f, context);
} else if (strncasecmp(cmd, "unmount", 7) == 0) {
Expand Down Expand Up @@ -240,15 +237,6 @@ void write_acl(txrx f, void *context) {
f(context, s);
}

/* Unlocks door lock for 5 seconds.
*
*/
void on_door_unlock(txrx f, void *context) {
if (mode == CONTROLLER) {
door_unlock(5000);
}
}

/* Displays a list of the supported commands.
*
*/
Expand Down
1 change: 1 addition & 0 deletions pico/core/include/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ typedef void (*txrx)(void *, const char *);
void cli_reboot(txrx, void *);
void cli_set_time(char *, txrx, void *);
void cli_blink(txrx, void *);
void cli_unlock_door(txrx, void *);
void keypad(char *, txrx, void *);
void set_passcodes(char *, txrx, void *);
10 changes: 10 additions & 0 deletions pico/core/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cli.h>
#include <led.h>
#include <logd.h>
#include <relays.h>
#include <sys.h>
#include <wiegand.h>
#include <write.h>
Expand Down Expand Up @@ -49,6 +50,15 @@ void cli_blink(txrx f, void *context) {
led_blink(5);
}

/* Unlocks door lock for 5 seconds.
*
*/
void cli_unlock_door(txrx f, void *context) {
if ((mode == READER) || (mode == CONTROLLER)) {
door_unlock(5000);
}
}

/* Sets the override passcodes.
*
*/
Expand Down
13 changes: 8 additions & 5 deletions pico/core/src/flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ const uint32_t ACL_MAGIC_WORD = 0xaa5555aa;
const uint32_t ACL_ALLOWED = 1;
const uint32_t ACL_DENIED = 0;
const uint32_t ACL_VERSION = 16384;
const uint32_t HEADER_SIZE = FLASH_PAGE_SIZE;
const uint32_t DATA_OFFSET = 128;
const uint32_t DATA_PREAMBLE = 128;
const uint32_t PASSCODES_OFFSET = 128;
const uint32_t CARDS_OFFSET = 256; // FLASH_PAGE_SIZE

typedef struct header {
uint32_t magic;
Expand All @@ -43,8 +46,8 @@ void flash_read_acl(CARD cards[], int *N, uint32_t passcodes[4]) {
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;
uint32_t *p = (uint32_t *)(addr + CARDS_OFFSET);
uint32_t *q = (uint32_t *)(addr + PASSCODES_OFFSET);
int ix = 0;

uint32_t *r = (uint32_t *)(addr);
Expand Down Expand Up @@ -139,7 +142,7 @@ void flash_write_acl(CARD cards[], int N, uint32_t passcodes[4]) {
header.magic = ACL_MAGIC_WORD;
header.version = version;
header.cards = count;
header.crc = crc32((char *)(&buffer[64]), count * 64);
header.crc = crc32((char *)(&buffer[32]), DATA_PREAMBLE + count * 64); // passcodes + cards

// ... copy header to buffer
buffer[0] = header.magic;
Expand Down Expand Up @@ -171,7 +174,7 @@ int flash_get_current_page() {
header.crc = *p++;

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

if (header.crc == crc) {
return index % PAGES;
Expand Down
12 changes: 1 addition & 11 deletions pico/reference/common/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ void query(txrx, void *);

void on_card_command(char *, handler, txrx, void *);

void on_door_unlock(txrx, void *);
void on_door_open(txrx, void *);
void on_door_close(txrx, void *);
void on_press_button(txrx, void *);
Expand Down Expand Up @@ -78,7 +77,7 @@ void execw(char *cmd, txrx f, void *context) {
} else if (strncasecmp(cmd, "blink", 5) == 0) {
cli_blink(f, context);
} else if (strncasecmp(cmd, "unlock", 6) == 0) {
on_door_unlock(f, context);
cli_unlock_door(f, context);
} else if (strncasecmp(cmd, "query", 5) == 0) {
query(f, context);
} else if (strncasecmp(cmd, "open", 4) == 0) {
Expand Down Expand Up @@ -382,15 +381,6 @@ void on_card_command(char *cmd, handler fn, txrx f, void *context) {
fn(facility_code, card, f, context);
}

/* Unlocks door lock for 5 seconds (READER/CONTROLLER mode only).
*
*/
void on_door_unlock(txrx f, void *context) {
if ((mode == READER) || (mode == CONTROLLER)) {
door_unlock(5000);
}
}

/* Door contact emulation command handler.
* Opens/closes the door contact emulation relay (in reader mode only).
*
Expand Down

0 comments on commit d71447f

Please sign in to comment.