diff --git a/README.md b/README.md index cf99512..3771f2b 100644 --- a/README.md +++ b/README.md @@ -43,5 +43,5 @@ Any legal trouble you recieve due to possessing a raw shim for KVS is not my res ## Credits kxtzownsu - writing KVS & KVG, porting to C
-hannah - writing the tpm2 api in `src/KVS/tpm.c`, moral support, testing
+hannah - writing the `is_ti50` tool, moral support, testing
people in TN - giving me the original kernver files before KVG existed
diff --git a/include/hex_utils.h b/include/hex_utils.h index 877491c..59ca7b9 100644 --- a/include/hex_utils.h +++ b/include/hex_utils.h @@ -11,6 +11,6 @@ uint32_t convert_to_uint32(const char *str); bool is_valid_hex(const char *str); void print_hex(const uint8_t *data, uint32_t size); -bool grep(char *string, const char *pattern); +void trim_newline(char* str); #endif // HEX_UTILS_H diff --git a/include/sysinfo.h b/include/sysinfo.h index ab78fe4..3bc95b4 100644 --- a/include/sysinfo.h +++ b/include/sysinfo.h @@ -11,13 +11,6 @@ const char *KERNVER_TYPE = "N/A. This is an error, please report at https://github.com/kxtzownsu/KVS with a picture of the screen."; -void trim_newline(char* str) { - size_t len = strlen(str); - if (len > 0 && str[len - 1] == '\n') { - str[len - 1] = '\0'; - } -} - const char* getFirmwareVersion(){ // note, may not work on all chromebooks @@ -28,15 +21,14 @@ const char* getFirmwareVersion(){ static char firmwareVersion[1024]; if (fptr == NULL) { - printf("Error reading Firmware Version \n"); - printf("Please report as a bug at https://github.com/kxtzownsu/KVS-private\n"); + printf("Error reading Firmware Version\n"); + printf("Please report as a bug at https://github.com/kxtzownsu/KVS\n"); sleep(86400); return "Error!"; } fgets(firmwareVersion, 100, fptr); fclose(fptr); - trim_newline(firmwareVersion); return firmwareVersion; @@ -45,17 +37,15 @@ const char* getFirmwareVersion(){ const char* getTpmVersion(){ char cmd[] = "tpmc tpmver"; static char output[5]; - FILE* fp = popen(cmd, "r"); fgets(output, sizeof(output), fp); fclose(fp); - trim_newline(output); return output; } -const char* getKernver() { +char* getKernver() { char cmd[] = "tpmc read 0x1008 9 2>/dev/null"; static char output[26]; FILE* fp = popen(cmd, "r"); @@ -83,7 +73,6 @@ const char* getKernver() { KERNVER_TYPE = "v0"; } - return kernver_str; } diff --git a/include/tpm.h b/include/tpm.h index 55941b6..4269a9b 100644 --- a/include/tpm.h +++ b/include/tpm.h @@ -3,6 +3,6 @@ #include int tpm_nvwrite(char* index, char* bytes); -int tpm_nvread(char* index, char* size); +const char* tpm_nvread(char* index, char* size); #endif \ No newline at end of file diff --git a/include/ui.h b/include/ui.h index cf99433..d54d771 100644 --- a/include/ui.h +++ b/include/ui.h @@ -5,5 +5,6 @@ void ui_flash(char* flashtype); void ui_header(const char* fwver, const char* kernver, const char* tpmver, const char* fwmp, const char* gscver, const char* gsctype); void show_credits(); void troll(); +void enterToContinue(); #endif \ No newline at end of file diff --git a/src/KVS/hex_utils.c b/src/KVS/hex_utils.c index e953c9c..ebd3774 100644 --- a/src/KVS/hex_utils.c +++ b/src/KVS/hex_utils.c @@ -27,6 +27,9 @@ void print_hex(const uint8_t *data, uint32_t size) { printf("\n"); } -bool grep(char *string, const char *pattern) { - return strstr(string, pattern) != NULL; +void trim_newline(char* str) { + size_t len = strlen(str); + if (len > 0 && str[len - 1] == '\n') { + str[len - 1] = '\0'; + } } diff --git a/src/KVS/main.c b/src/KVS/main.c index 58c8ab9..7e96257 100644 --- a/src/KVS/main.c +++ b/src/KVS/main.c @@ -26,19 +26,21 @@ int main(int argc, char **argv) { // example values for testing const char* fwver = getFirmwareVersion(); - const char* kernver = getKernver(); const char* tpmver = getTpmVersion(); const char* fwmp = getFWMPFlags(); const char* gscver = getGSCRWVersion(); const char* gsctype = getGSCType(); - const char* requirement_flash = "[UNENROLLED]"; // only allow 2 characters (option & newline) char choice[3]; while (true) { + char* kernver = getKernver(); + + printf("\033[H\033[J"); // clears the screen + ui_header(fwver, kernver, tpmver, fwmp, gscver, gsctype); - printf("%s 1) Flash new kernver \n", requirement_flash); + printf("1) Flash new kernver \n"); printf("2) Run KAUB (Kernver Automatic Update Blocker) \n"); printf("3) Kernver FAQ \n"); printf("4) Credits\n"); @@ -52,14 +54,19 @@ int main(int argc, char **argv) { if (!strcmp(choice, "1")) { ui_flash("tpm0"); + enterToContinue(); } else if (!strcmp(choice, "2")) { - printf("KAUB is not avaliable on v2.0.0. Please either update your shim or wait for KAUB to release on v2.1"); + printf("KAUB is not avaliable on v2.0.0. Please either update your shim or wait for KAUB to release on v2.1\n"); + enterToContinue(); } else if (!strcmp(choice, "3")) { kernver_faq(); + enterToContinue(); } else if (!strcmp(choice, "4")) { show_credits(); - } else if (!strcmp(choice, "4")) { + enterToContinue(); + } else if (!strcmp(choice, "5")) { system("/bin/bash"); + enterToContinue(); } else if (!strcmp(choice, "6")) { exit(1); } else if (!strcmp(choice, "7")) { diff --git a/src/KVS/tpm.c b/src/KVS/tpm.c index 8e34df9..2305c68 100644 --- a/src/KVS/tpm.c +++ b/src/KVS/tpm.c @@ -1,23 +1,64 @@ #include #include +#include + +#include "hex_utils.h" + +int calculateByteSize(char* bytes){ + int byte_count = 0; + char bytes_copy[256]; + strncpy(bytes_copy, bytes, sizeof(bytes_copy) - 1); + bytes_copy[sizeof(bytes_copy) - 1] = '\0'; + + char* token = strtok(bytes_copy, " "); + while (token != NULL) { + byte_count++; + token = strtok(NULL, " "); + } + return byte_count; +} + -// yeah kill me but this is just a `tpmc` wrapper :troll: /* ARGS: -index = that what TPM2 index to read from, e.g: "0x1008" -size = how many bytes should we read +index = what TPM2 index to read from, e.g: "0x1008" +bytes = what bytes to write to `index`, e.g: "02 4c" + +EXAMPLE: +tpm_nvwrite("0x1008", "02 4c"); + ⤷ returns int with amount of bytes written, e.g: "2" */ -int tpm_nvwrite(char* index, char* bytes){ - printf ("wip, index: %s, bytes: '%s'", index, bytes); - return 0; +int tpm_nvwrite(char* index, char* bytes) { + char cmd[2048]; + char output[1024]; + + snprintf(cmd, sizeof(cmd), "tpmc write %s %s 2>/dev/null", index, bytes); + FILE* fp = popen(cmd, "r"); + fgets(output, sizeof(output), fp); + fclose(fp); + + return calculateByteSize(bytes); } /* ARGS: -index = that what TPM2 index to read from, e.g: "0x1008" -size = how many bytes should we read +index = what TPM2 index to read from, e.g: "0x1008" +size = how many bytes should we read, e.g: "2" + +EXAMPLE: +tpm_nvread("0x1008", "2"); + ⤷ returns char with bytes read, e.g: "02 4c" */ -int tpm_nvread(char* index, char* size){ - printf ("wip, index: %s, size: '%s'", index, size); - return 0; +char* tpm_nvread(char* index, char* size) { + int intSize = strtol(size, NULL, 10); + char cmd[2048]; + static char output[1024]; + + snprintf(cmd, sizeof(cmd), "tpmc read %s 0x%X", index, intSize); + FILE* fp = popen(cmd, "r"); + fgets(output, sizeof(output), fp); + fclose(fp); + trim_newline(output); + + return output; } \ No newline at end of file diff --git a/src/KVS/ui.c b/src/KVS/ui.c index 676d0a3..711a49c 100644 --- a/src/KVS/ui.c +++ b/src/KVS/ui.c @@ -60,6 +60,7 @@ void ui_flash(char* flashtype) { if (flashtype == "tpm0"){ if (!strcmp(KERNVER_TYPE, "v0")) { tpm_nvwrite("0x1008", kvgout_v0); + } else if (!strcmp(KERNVER_TYPE, "v1")) { tpm_nvwrite("0x1008", kvgout_v1); } @@ -68,7 +69,7 @@ void ui_flash(char* flashtype) { } } -void ui_header(const char* fwver, const char* kernver, const char* tpmver, const char* fwmp, const char* gscver, const char* gsctype){ +void ui_header(const char* fwver, char* kernver, const char* tpmver, const char* fwmp, const char* gscver, const char* gsctype){ printf("KVS: Kernel Version Switcher (codename Maglev, bid: 2.0.0))\n"); printf("FW Version: %s\n", fwver); printf("Kernel Version: %s\n", kernver); @@ -108,4 +109,9 @@ void troll(){ ); sleep(1); } +} + +void enterToContinue() { + printf("\nPress ENTER to return to the main menu\n"); + getchar(); } \ No newline at end of file