/* Saved WiFi networks: a small fixed-capacity list in one 8.3 FAT file. * Everything is read/written whole; the network menu and the boot helper * both go through this module. */ #include "ucmd_lib.h" #include "wifi_store.h" #define CFG_SLACK 512 /* FAT may write a final partial sector */ static void wipe(void *p, unsigned int n) { volatile unsigned char *b = (volatile unsigned char *)p; while (n--) *b++ = 0; } static int cfg_valid(const struct wifi_cfg *c) { return c->magic == WIFI_CFG_MAGIC && c->ssid_len >= 1 && c->ssid_len <= 32 && c->pass_len <= 64; } int wifi_store_find(const struct wifi_cfg *list, int count, const unsigned char *ssid, unsigned int ssid_len) { int i; if (!list || !ssid || ssid_len == 0 || ssid_len > 32) return -1; for (i = 0; i < count; i++) { unsigned int j; if (list[i].ssid_len != ssid_len) continue; for (j = 0; j < ssid_len; j++) if (list[i].ssid[j] != ssid[j]) break; if (j == ssid_len) return i; } return -1; } int wifi_store_load(struct wifi_cfg *out, int max) { union { struct wifi_cfg_list list; struct wifi_cfg one; unsigned char raw[sizeof(struct wifi_cfg_list) + CFG_SLACK]; } buf; int r, n = 0, i; if (!out || max <= 0) return 0; r = u_syscall3(SYS_READ_FILE, (int)WIFI_CRED_PATH, (int)buf.raw, (int)sizeof(buf.raw)); if (r >= 4 && buf.list.magic == WIFI_CFG_LIST_MAGIC) { int count; if (r < 12 || buf.list.version != WIFI_CFG_LIST_VER) goto done; count = (int)buf.list.count; if (count > WIFI_CFG_MAX) count = WIFI_CFG_MAX; if ((unsigned int)r < 12u + (unsigned int)count * sizeof(struct wifi_cfg)) count = (r - 12) / (int)sizeof(struct wifi_cfg); for (i = 0; i < count && n < max; i++) { const struct wifi_cfg *c = &buf.list.net[i]; if (!cfg_valid(c)) continue; if (wifi_store_find(out, n, c->ssid, c->ssid_len) >= 0) continue; out[n++] = *c; } } else if (r >= (int)sizeof(struct wifi_cfg) && cfg_valid(&buf.one)) { out[0] = buf.one; n = 1; } done: wipe(buf.raw, sizeof(buf.raw)); return n; } int wifi_store_save(const struct wifi_cfg *list, int count) { union { struct wifi_cfg_list list; unsigned char raw[sizeof(struct wifi_cfg_list) + CFG_SLACK]; } buf; unsigned int len; int i, r; if (!list || count <= 0) { u_syscall1(SYS_UNLINK, (int)WIFI_CRED_PATH); return 0; } if (count > WIFI_CFG_MAX) count = WIFI_CFG_MAX; wipe(buf.raw, sizeof(buf.raw)); buf.list.magic = WIFI_CFG_LIST_MAGIC; buf.list.version = WIFI_CFG_LIST_VER; buf.list.count = (unsigned int)count; for (i = 0; i < count; i++) buf.list.net[i] = list[i]; len = 12u + (unsigned int)count * sizeof(struct wifi_cfg); r = u_syscall3(SYS_WRITE_FILE, (int)WIFI_CRED_PATH, (int)buf.raw, (int)len); wipe(buf.raw, sizeof(buf.raw)); return r == 0 ? 0 : -1; } int wifi_store_remember(const struct wifi_cfg *rec) { struct wifi_cfg list[WIFI_CFG_MAX]; int n, i, idx, r; if (!rec || !cfg_valid(rec)) return -1; n = wifi_store_load(list, WIFI_CFG_MAX); idx = wifi_store_find(list, n, rec->ssid, rec->ssid_len); if (idx >= 0) { for (i = idx; i + 1 < n; i++) list[i] = list[i + 1]; n--; } if (n >= WIFI_CFG_MAX) n = WIFI_CFG_MAX - 1; /* drop the least recently used */ for (i = n; i > 0; i--) list[i] = list[i - 1]; list[0] = *rec; list[0].magic = WIFI_CFG_MAGIC; r = wifi_store_save(list, n + 1); wipe(list, sizeof(list)); return r; } int wifi_store_forget(const unsigned char *ssid, unsigned int ssid_len) { struct wifi_cfg list[WIFI_CFG_MAX]; int n, i, idx, r; n = wifi_store_load(list, WIFI_CFG_MAX); idx = wifi_store_find(list, n, ssid, ssid_len); if (idx < 0) { wipe(list, sizeof(list)); return 0; } for (i = idx; i + 1 < n; i++) list[i] = list[i + 1]; n--; r = wifi_store_save(list, n) == 0 ? 1 : -1; wipe(list, sizeof(list)); return r; }