more regnual

This commit is contained in:
NIIBE Yutaka
2012-05-18 16:54:17 +09:00
parent 87d36deeb9
commit c723df0841
8 changed files with 71 additions and 39 deletions

View File

@@ -80,7 +80,6 @@ CSRC = $(PORTSRC) \
$(BOARD_DIR)/board.c \
$(CHIBIOS)/os/various/evtimer.c \
$(CHIBIOS)/os/various/syscalls.c \
$(VCOMSRC) \
$(CRYPTSRC) \
main.c usb_lld.c \
usb_desc.c usb_prop.c \
@@ -195,13 +194,6 @@ ULIBS =
# End of user defines
##############################################################################
ifeq ($(USE_FWLIB),yes)
include $(CHIBIOS)/ext/stm32lib/stm32lib.mk
CSRC += $(STM32SRC)
INCDIR += $(STM32INC)
USE_OPT += -DUSE_STDPERIPH_DRIVER
endif
include $(CHIBIOS)/os/ports/GCC/ARM/rules.mk
MCFLAGS= -mcpu=$(MCU) -mfix-cortex-m3-ldrd

View File

@@ -389,4 +389,4 @@ extern int pinpad_getline (int msg_code, systime_t timeout);
#endif
extern uint8_t __heap_base__, __heap_end__;
extern uint8_t _regnual_start, __heap_end__;

View File

@@ -114,6 +114,8 @@ SECTIONS
PROVIDE(end = .);
_end = .;
. = ALIGN(256);
_regnual_start = .;
@CERTDO_SUPPORT_START@
.gnuk_ch_certificate :

View File

@@ -375,7 +375,7 @@ led_blink (int spec)
* See the hwinit1_common function.
*/
int
main (int argc, char **argv)
main (int argc, char *argv[])
{
int count = 0;
@@ -418,6 +418,9 @@ main (int argc, char **argv)
{
eventmask_t m;
if (icc_state_p != NULL && *icc_state_p == ICC_STATE_EXEC_REQUESTED)
break;
count++;
m = chEvtWaitOneTimeout (ALL_EVENTS, LED_TIMEOUT_INTERVAL);
switch (m)
@@ -469,6 +472,10 @@ main (int argc, char **argv)
#endif
}
usb_lld_shutdown ();
/* erase by mass erase */
/* set vector */
/* SYSRESETREQ to invoke regnual */
return 0;
}

View File

@@ -75,7 +75,6 @@ struct usb_device_method
};
enum {
USB_EVENT_RESET,
USB_EVENT_ADDRESS,
USB_EVENT_CONFIG,
USB_EVENT_SUSPEND,

View File

@@ -189,7 +189,7 @@ static const uint8_t data_rate_table[] = { 0x80, 0x25, 0, 0, }; /* dwDataRate */
static const uint8_t lun_table[] = { 0, 0, 0, 0, };
#endif
static const uint8_t *mem_info[] = { &__heap_base__, &__heap_end__, };
static const uint8_t *mem_info[] = { &_regnual_start, &__heap_end__, };
#define USB_FSIJ_GNUK_MEMINFO 0
#define USB_FSIJ_GNUK_DOWNLOAD 1
@@ -225,7 +225,7 @@ gnuk_setup (uint8_t req, uint8_t req_no,
if (icc_state_p == NULL || *icc_state_p != ICC_STATE_EXITED)
return USB_UNSUPPORT;
if (addr < &__heap_base__ || addr + len > &__heap_end__)
if (addr < &_regnual_start || addr + len > &__heap_end__)
return USB_UNSUPPORT;
if (index == 0)
@@ -349,11 +349,9 @@ static int gnuk_usb_event (uint8_t event_type, uint16_t value)
switch (event_type)
{
case USB_EVENT_RESET:
break;
case USB_EVENT_ADDRESS:
bDeviceState = ADDRESSED;
break;
return USB_SUCCESS;
case USB_EVENT_CONFIG:
if (usb_lld_current_configuration () == 0)
{
@@ -365,7 +363,6 @@ static int gnuk_usb_event (uint8_t event_type, uint16_t value)
gnuk_setup_endpoints_for_interface (i, 0);
bDeviceState = CONFIGURED;
chEvtSignalI (main_thread, LED_STATUS_MODE);
return USB_SUCCESS;
}
else
{
@@ -377,6 +374,7 @@ static int gnuk_usb_event (uint8_t event_type, uint16_t value)
gnuk_setup_endpoints_for_interface (i, 1);
bDeviceState = ADDRESSED;
}
return USB_SUCCESS;
default:
break;
}

View File

@@ -75,7 +75,7 @@ class gnuk_token:
self.__alt = interface.alternateSetting
self.__conf = configuration
self.__bulkout = 2
self.__bulkout = 1
self.__bulkin = 0x81
self.__timeout = 10000

View File

@@ -46,7 +46,7 @@ def iso7816_compose(ins, p1, p2, data):
if data_len == 0:
return pack('>BBBB', cls, ins, p1, p2)
else:
return pack('>BBBBBh', cls, ins, p1, p2, 0, data_len) + data
return pack('>BBBBB', cls, ins, p1, p2, data_len) + data
# This class only supports Gnuk (for now)
class gnuk_token:
@@ -74,16 +74,28 @@ class gnuk_token:
self.__alt = interface.alternateSetting
self.__conf = configuration
self.__bulkout = 2
self.__bulkout = 1
self.__bulkin = 0x81
self.__timeout = 10000
self.__seq = 0
def stop_icc(self):
# XXX: need to disclaim interface and close device and re-open???
# self.__devhandle.setConfiguration(0)
return
def mem_info(self):
mem = self.__devhandle.controlMsg(requestType = 0xc0, request = 0,
value = 0, index = 0, buffer = 8,
timeout = 10)
start = ((mem[3]*256 + mem[2])*256 + mem[1])*256 + mem[0]
end = ((mem[7]*256 + mem[6])*256 + mem[5])*256 + mem[4]
return (start, end)
def icc_get_result(self):
msg = self.__devhandle.bulkRead(self.__bulkin, 1024, self.__timeout)
if len(msg) < 10:
print msg
raise ValueError, "icc_get_result"
msg_type = msg[0]
data_len = msg[1] + (msg[2]<<8) + (msg[3]<<16) + (msg[4]<<24)
@@ -150,33 +162,42 @@ class gnuk_token:
raise ValueError, "icc_send_cmd"
def cmd_get_response(self, expected_len):
cmd_data = iso7816_compose(0xc0, 0x00, 0x00, [expected_len])
cmd_data = iso7816_compose(0xc0, 0x00, 0x00, '') + pack('>B', expected_len)
response = self.icc_send_cmd(cmd_data)
return response
return response[:-2]
def cmd_verify(self, who, passwd):
cmd_data = iso7816_compose(0x20, 0x00, 0x80+who, passwd)
sw = self.icc_send_cmd(cmd_data)
if len(sw) != 2:
raise ValueError, "cmd_verify"
raise ValueError, sw
if not (sw[0] == 0x90 and sw[1] == 0x00):
raise ValueError, "cmd_verify"
raise ValueError, sw
def cmd_select_openpgp(self):
cmd_data = iso7816_compose(0xa4, 0x04, 0x0c, "\xD2\x76\x00\x01\x24\x01")
sw = self.icc_send_cmd(cmd_data)
if len(sw) != 2:
raise ValueError, "cmd_select_openpgp"
raise ValueError, sw
if not (sw[0] == 0x90 and sw[1] == 0x00):
raise ValueError, ("%02x%02x" % (sw[0], sw[1]))
def cmd_external_authenticate(self, who, signed):
def cmd_external_authenticate(self, signed):
cmd_data = iso7816_compose(0x82, 0x00, 0x00, signed)
sw = self.icc_send_cmd(cmd_data)
if len(sw) != 2:
raise ValueError, "cmd_external_authenticate"
raise ValueError, sw
if not (sw[0] == 0x90 and sw[1] == 0x00):
raise ValueError, "cmd_external_authenticate"
raise ValueError, ("%02x%02x" % (sw[0], sw[1]))
def cmd_get_challenge(self):
cmd_data = iso7816_compose(0x84, 0x00, 0x00, '')
sw = self.icc_send_cmd(cmd_data)
if len(sw) != 2:
raise ValueError, sw
if sw[0] != 0x61:
raise ValueError, ("%02x%02x" % (sw[0], sw[1]))
return self.cmd_get_response(sw[1])
def compare(data_original, data_in_device):
i = 0
@@ -199,7 +220,13 @@ def get_device():
return dev, config, alt
raise ValueError, "Device not found"
def main(passwd, data):
def to_string(t):
result = ""
for c in t:
result += chr(c)
return result
def main(passwd, data_regnual, data_upgrade):
dev, config, intf = get_device()
print "Device: ", dev.filename
print "Configuration: ", config.value
@@ -212,14 +239,17 @@ def main(passwd, data):
icc.cmd_verify(3, passwd)
icc.cmd_select_openpgp()
challenge = icc.cmd_get_challenge()
signed = challenge
signed = to_string(challenge)
icc.cmd_external_authenticate(signed)
# set_configure(0) to disable all interfaces but control pipe
# check mem_info
icc.stop_icc() # disable all interfaces but control pipe
mem_info icc.mem_info()
print "%08x: %08x" % mem_info
# download flash install program
# exec
# ...
print "Downloading flash upgrade program"
# Then, send upgrade program...
print "Downloading the program"
return 0
DEFAULT_PW3 = "12345678"
@@ -230,10 +260,14 @@ if __name__ == '__main__':
from getpass import getpass
passwd = getpass("Admin password: ")
sys.argv.pop(1)
filename = sys.argv[1]
f = open(filename)
data = f.read()
filename_regnual = sys.argv[1]
filename_upgrade = sys.argv[2]
f = open(filename_regnual)
data_regnual = f.read()
f.close()
print "%s: %d" % (filename, len(data))
print "Downloading flash upgrade program"
main(passwd, data)
print "%s: %d" % (filename_regnual, len(data_regnual))
f = open(filename_upgrade)
data_upgrade = f.read()
f.close()
print "%s: %d" % (filename_upgrade, len(data_upgrade))
main(passwd, data_regnual, data_upgrade)