From 21a46306ae5534102f0499f6246eb1bfb2491e5a Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Thu, 18 Feb 2021 14:04:21 +0900 Subject: [PATCH] Make GNU/Linux emulation work again, with initialization support. Signed-off-by: NIIBE Yutaka --- ChangeLog | 11 +++++++++++ GNUK_USB_DEVICE_ID | 2 +- src/configure | 8 +++++--- src/main.c | 41 +++++++++++++++++++++++++++++++++++++---- 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 048169f..d746f9b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2021-02-18 NIIBE Yutaka + + * src/configure [GNU_LINUX_EMULATION] (def_mhz): Define. + (output_vendor_product_serial_strings): Output ARCH. + + * GNUK_USB_DEVICE_ID: Use "Gnuk Token" for emulation, too. + + * src/main.c [GNU_LINUX_EMULATION] (main): Add initialization of + the .gnuk-flash-image file. + (gnuk_sbrk): Rename from sbrk. + 2020-09-10 NIIBE Yutaka * VERSION: 1.2.16. diff --git a/GNUK_USB_DEVICE_ID b/GNUK_USB_DEVICE_ID index 47bb67a..da87dac 100644 --- a/GNUK_USB_DEVICE_ID +++ b/GNUK_USB_DEVICE_ID @@ -1,5 +1,5 @@ # VID:PID bcdDev Product_STRING Vendor_STRING -0000:0000 0200 Gnuk Emulation Free Software Initiative of Japan +0000:0000 0200 Gnuk Token Free Software Initiative of Japan 234b:0000 0200 Gnuk Token Free Software Initiative of Japan 20a0:4211 0200 Nitrokey Start Nitrokey 1209:2440 0200 Gnuk Token GnuPG e.V. diff --git a/src/configure b/src/configure index d09cde6..24d6ecb 100755 --- a/src/configure +++ b/src/configure @@ -6,7 +6,7 @@ nl=$'\n' # # This file is *NOT* generated by GNU Autoconf, but written by NIIBE Yutaka # -# Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 +# Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2021 # Free Software Initiative of Japan # # This file is a part of Gnuk, a GnuPG USB Token implementation. @@ -210,26 +210,27 @@ FST_01SZ) ;; esac +def_mhz="-DMHZ=$MHZ" if test "$target" = "GNU_LINUX"; then ldscript="" chip="gnu-linux" + arch="gnu-linux" emulation="yes" cross="" mcu="none" def_emulation="-DGNU_LINUX_EMULATION" def_memory_size="-DMEMORY_SIZE=1024" - def_mhz="" enable_hexoutput="" libs="-lpthread" else ldscript="gnuk.ld" chip="stm32f103" + arch="cortex-m" emulation="" cross="arm-none-eabi-" mcu="cortex-m3" def_emulation="" def_memory_size="-DMEMORY_SIZE=$MEMORY_SIZE" - def_mhz="-DMHZ=$MHZ" enable_hexoutput=yes libs="" fi @@ -470,6 +471,7 @@ fi (echo "CHIP=$chip"; + echo "ARCH=$arch"; echo "EMULATION=$emulation"; echo "CROSS=$cross"; echo "MCU=$mcu"; diff --git a/src/main.c b/src/main.c index 88de8d3..988a35a 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,7 @@ /* * main.c - main routine of Gnuk * - * Copyright (C) 2010, 2011, 2012, 2013, 2015, 2016, 2017, 2018 + * Copyright (C) 2010, 2011, 2012, 2013, 2015, 2016, 2017, 2018, 2021 * Free Software Initiative of Japan * Author: NIIBE Yutaka * @@ -36,6 +36,11 @@ #include "usb-cdc.h" #include "random.h" #ifdef GNU_LINUX_EMULATION +#include +#include +#include +#include + #include #include #define main emulated_main @@ -372,6 +377,34 @@ main (int argc, const char *argv[]) else flash_image_path = argv[1]; + if (access (flash_image_path, F_OK) < 0) + { + int fd; + char buf[8192]; + + memset (buf, 0xff, sizeof buf); + memset (buf+4*1024, 0, 2); + fd = open (flash_image_path, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR); + if (fd < 0) + { + perror ("creating flash file"); + exit (1); + } + + if (write (fd, buf, sizeof buf) != sizeof buf) + { + perror ("initializing flash file"); + close (fd); + exit (1); + } + + close (fd); + } + + puts ("Gnuk (emulation with USBIP), a GnuPG USB Token implementation"); + puts ("Copyright (C) 2021 Free Software Initiative of Japan"); + puts ("This is free software under GPLv3+."); + flash_addr = flash_init (flash_image_path); flash_addr_key_storage_start = (uint8_t *)flash_addr; flash_addr_data_storage_start = (uint8_t *)flash_addr + 4096; @@ -563,11 +596,11 @@ gnuk_malloc_init (void) } static void * -sbrk (size_t size) +gnuk_sbrk (intptr_t size) { void *p = (void *)heap_p; - if ((size_t)(HEAP_END - heap_p) < size) + if ((HEAP_END - heap_p) < size) return NULL; heap_p += size; @@ -603,7 +636,7 @@ gnuk_malloc (size_t size) { if (m == NULL) { - m = (struct mem_head *)sbrk (size); + m = (struct mem_head *)gnuk_sbrk (size); if (m) m->size = size; break;