serial number handling by ROM

This commit is contained in:
NIIBE Yutaka
2011-05-11 10:56:36 +09:00
parent 661c8e63dc
commit 93a7515bcd
6 changed files with 59 additions and 38 deletions

View File

@@ -1,5 +1,14 @@
2011-05-11 NIIBE Yutaka <gniibe@fsij.org> 2011-05-11 NIIBE Yutaka <gniibe@fsij.org>
* src/main.c (device_initialize_once): New.
* src/usb_prop.c (gnukStringSerial): Move to...
* src/usb_desc.c (gnukStringSerial): here. Bump version to 0.12.
Fill by 0xff.
* src/usb_prop.c (gnuk_device_init)
(gnuk_device_GetStringDescriptor): Don't use RAM for
gnukStringSerial, use ROM like other string descriptor.
* src/usb_desc.c (String_Descriptor): Add gnukStringSerial.
* src/openpgp-do.c (gpg_get_pw1_lifetime): Make static. * src/openpgp-do.c (gpg_get_pw1_lifetime): Make static.
(gpg_do_load_prvkey, gpg_do_write_prvkey): Use kdi. (gpg_do_load_prvkey, gpg_do_write_prvkey): Use kdi.
(gpg_increment_digital_signature_counter): Call gpg_get_pw1_lifetime. (gpg_increment_digital_signature_counter): Call gpg_get_pw1_lifetime.

View File

@@ -5,7 +5,3 @@
@DFU_DEFINE@ @DFU_DEFINE@
@PINPAD_DEFINE@ @PINPAD_DEFINE@
@PINPAD_MORE_DEFINE@ @PINPAD_MORE_DEFINE@
#if !defined(__ASSEMBLER__)
extern const unsigned char *unique_device_id (void);
#endif

View File

@@ -326,6 +326,9 @@ extern void flash_bool_write_internal (const uint8_t *p, int nr);
extern void flash_cnt123_write_internal (const uint8_t *p, int which, int v); extern void flash_cnt123_write_internal (const uint8_t *p, int which, int v);
extern void flash_do_write_internal (const uint8_t *p, int nr, const uint8_t *data, int len); extern void flash_do_write_internal (const uint8_t *p, int nr, const uint8_t *data, int len);
extern const unsigned char *unique_device_id (void);
extern const uint8_t gnukStringSerial[];
#if defined(PINPAD_SUPPORT) #if defined(PINPAD_SUPPORT)
#if defined(PINPAD_CIR_SUPPORT) #if defined(PINPAD_CIR_SUPPORT)
extern void cir_ext_disable (void); extern void cir_ext_disable (void);

View File

@@ -164,6 +164,36 @@ extern msg_t USBthread (void *arg);
#define LED_TIMEOUT_STOP MS2ST(500) #define LED_TIMEOUT_STOP MS2ST(500)
#define ID_OFFSET 12
static void
device_initialize_once (void)
{
const uint8_t *p = &gnukStringSerial[ID_OFFSET];
if (p[0] == 0xff && p[1] == 0xff && p[2] == 0xff && p[3] == 0xff)
{
/*
* This is the first time invocation.
* Setup serial number by unique device ID.
*/
const uint8_t *u = unique_device_id ();
int i;
for (i = 0; i < 4; i++)
{
uint8_t b = u[i];
uint8_t nibble;
nibble = (b >> 4);
nibble += (nibble >= 10 ? ('A' - 10) : '0');
flash_put_data_internal (&p[i*4], nibble << 8);
nibble = (b & 0x0f);
nibble += (nibble >= 10 ? ('A' - 10) : '0');
flash_put_data_internal (&p[i*4+2], nibble << 8);
}
}
}
static volatile uint8_t fatal_code; static volatile uint8_t fatal_code;
/* /*
@@ -180,6 +210,7 @@ main (int argc, char **argv)
(void)argc; (void)argc;
(void)argv; (void)argv;
device_initialize_once ();
usb_lld_init (); usb_lld_init ();
USB_Init(); USB_Init();

View File

@@ -224,6 +224,16 @@ static const uint8_t gnukStringProduct[] = {
' ', 0, 'T', 0, 'o', 0, 'k', 0, 'e', 0, 'n', 0 ' ', 0, 'T', 0, 'o', 0, 'k', 0, 'e', 0, 'n', 0
}; };
const uint8_t gnukStringSerial[] = {
13*2+2, /* bLength */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
'0', 0, '.', 0, '1', 0, '2', 0, /* Version number of Gnuk */
'-', 0,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
};
const ONE_DESCRIPTOR Device_Descriptor = { const ONE_DESCRIPTOR Device_Descriptor = {
(uint8_t*)gnukDeviceDescriptor, (uint8_t*)gnukDeviceDescriptor,
sizeof (gnukDeviceDescriptor) sizeof (gnukDeviceDescriptor)
@@ -234,8 +244,9 @@ const ONE_DESCRIPTOR Config_Descriptor = {
sizeof (gnukConfigDescriptor) sizeof (gnukConfigDescriptor)
}; };
const ONE_DESCRIPTOR String_Descriptor[3] = { const ONE_DESCRIPTOR String_Descriptor[] = {
{(uint8_t*)gnukStringLangID, sizeof (gnukStringLangID)}, {(uint8_t*)gnukStringLangID, sizeof (gnukStringLangID)},
{(uint8_t*)gnukStringVendor, sizeof (gnukStringVendor)}, {(uint8_t*)gnukStringVendor, sizeof (gnukStringVendor)},
{(uint8_t*)gnukStringProduct, sizeof (gnukStringProduct)}, {(uint8_t*)gnukStringProduct, sizeof (gnukStringProduct)},
{(uint8_t*)gnukStringSerial, sizeof (gnukStringSerial)},
}; };

View File

@@ -36,30 +36,10 @@
#include "usb-cdc-vport.c" #include "usb-cdc-vport.c"
#endif #endif
static uint8_t gnukStringSerial[] = {
13*2+2, /* bLength */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
'0', 0, '.', 0, '1', 0, '1', 0, /* Version number of Gnuk */
'-', 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
};
#define ID_OFFSET 12
static void static void
gnuk_device_init (void) gnuk_device_init (void)
{ {
const uint8_t *u = unique_device_id ();
int i;
for (i = 0; i < 4; i++)
{
gnukStringSerial[i*4+ID_OFFSET+0] = (u[i*2] >> 4) + 'A';
gnukStringSerial[i*4+ID_OFFSET+1] = 0;
gnukStringSerial[i*4+ID_OFFSET+2] = (u[i*2+1] & 0x0f) + 'A';
gnukStringSerial[i*4+ID_OFFSET+3] = 0;
}
pInformation->Current_Configuration = 0; pInformation->Current_Configuration = 0;
/* Connect the device */ /* Connect the device */
@@ -205,22 +185,13 @@ static uint8_t *
gnuk_device_GetStringDescriptor (uint16_t Length) gnuk_device_GetStringDescriptor (uint16_t Length)
{ {
uint8_t wValue0 = pInformation->USBwValue0; uint8_t wValue0 = pInformation->USBwValue0;
uint32_t wOffset = pInformation->Ctrl_Info.Usb_wOffset;
if (wValue0 == 3) if (wValue0 > (sizeof (String_Descriptor) / sizeof (ONE_DESCRIPTOR)))
/* Serial number is requested */
if (Length == 0)
{
pInformation->Ctrl_Info.Usb_wLength = sizeof gnukStringSerial - wOffset;
return 0;
}
else
return gnukStringSerial + wOffset;
else if (wValue0 > (sizeof (String_Descriptor) / sizeof (ONE_DESCRIPTOR)))
return NULL; return NULL;
else else
return Standard_GetDescriptorData (Length, return
(PONE_DESCRIPTOR)&String_Descriptor[wValue0]); Standard_GetDescriptorData (Length,
(PONE_DESCRIPTOR)&String_Descriptor[wValue0]);
} }
#ifdef ENABLE_VIRTUAL_COM_PORT #ifdef ENABLE_VIRTUAL_COM_PORT