From 5c47d31929dc4b1e993d34ad557d63585bc8ac42 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Tue, 17 Nov 2020 20:13:47 -0500 Subject: add warnings if any registers are not properly read --- genkey.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/genkey.c b/genkey.c index ef19e9e..8bb224a 100644 --- a/genkey.c +++ b/genkey.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include "deonebook.h" @@ -33,14 +34,14 @@ static unsigned char twiddle(unsigned char buf[2]) return c; } -static void get_sd_register(const char *dev, const char *reg, size_t n, unsigned char buf[n]) +static bool get_sd_register(const char *dev, const char *reg, size_t n, unsigned char buf[n]) { char path[256]; snprintf(path, sizeof(path), "/sys/block/%s/device/%s", dev, reg); FILE *f = fopen(path, "r"); if (f == NULL) { - return; + return false; } for (size_t i = 0; i < n; i++) { @@ -50,6 +51,7 @@ static void get_sd_register(const char *dev, const char *reg, size_t n, unsigned } fclose(f); + return true; } unsigned char * genkey(const char *dev) @@ -57,14 +59,28 @@ unsigned char * genkey(const char *dev) static unsigned char key[KEY_SIZE]; const unsigned char keycode[] = "eone"; size_t codelen = sizeof(keycode) - 1; + bool allread = true; unsigned char scr[SCR_SIZE] = "testtest"; unsigned char csd[CSD_SIZE] = "testtesttesttest"; unsigned char cid[CID_SIZE] = "testtesttesttest"; - get_sd_register(dev, "cid", sizeof(cid), cid); - get_sd_register(dev, "csd", sizeof(csd), csd); - get_sd_register(dev, "scr", sizeof(scr), scr); + if (!get_sd_register(dev, "cid", sizeof(cid), cid)) { + fprintf(stderr, "warning: CID register not read\n"); + allread = false; + } + if (!get_sd_register(dev, "csd", sizeof(csd), csd)) { + fprintf(stderr, "warning: CSD register not read\n"); + allread = false; + } + if (!get_sd_register(dev, "scr", sizeof(scr), scr)) { + fprintf(stderr, "warning: SCR register not read\n"); + allread = false; + } + + if (!allread) { + fprintf(stderr, "warning: key may be incorrect\n"); + } memcpy(key, cid + 3, 10); memcpy(key + 10, csd + 7, 3); -- cgit v1.2.1