1
0
Fork 0
forked from len0rd/rockbox

hwstub: enhance protocol with more functions

Change-Id: I7944249c2f7ea3e180e7b79ee8ae402d1d0742d3
This commit is contained in:
Amaury Pouly 2013-07-13 17:38:34 +02:00
parent 140783ef66
commit f617da0552
4 changed files with 71 additions and 70 deletions

View file

@ -26,8 +26,8 @@
#define HWSTUB_PROTOCOL 0x1d #define HWSTUB_PROTOCOL 0x1d
#define HWSTUB_VERSION_MAJOR 2 #define HWSTUB_VERSION_MAJOR 2
#define HWSTUB_VERSION_MINOR 9 #define HWSTUB_VERSION_MINOR 11
#define HWSTUB_VERSION_REV 2 #define HWSTUB_VERSION_REV 1
#define HWSTUB_USB_VID 0xfee1 #define HWSTUB_USB_VID 0xfee1
#define HWSTUB_USB_PID 0xdead #define HWSTUB_USB_PID 0xdead
@ -46,17 +46,18 @@
#define HWSTUB_RW_MEM 2 /* optional */ #define HWSTUB_RW_MEM 2 /* optional */
#define HWSTUB_CALL 3 /* optional */ #define HWSTUB_CALL 3 /* optional */
#define HWSTUB_JUMP 4 /* optional */ #define HWSTUB_JUMP 4 /* optional */
#define HWSTUB_AES_OTP 5 /* optional */ #define HWSTUB_STOP 5 /* optional */
/** /**
* HWSTUB_GET_INFO: get some information about an aspect of the device. * HWSTUB_GET_INFO: get some information about an aspect of the device.
* The wIndex field of the SETUP specifies which information to get. */ * The wIndex field of the SETUP specifies which information to get. */
/* list of possible information */ /* list of possible information */
#define HWSTUB_INFO_VERSION 0 #define HWSTUB_INFO_VERSION 0 /* mandatory */
#define HWSTUB_INFO_LAYOUT 1 #define HWSTUB_INFO_LAYOUT 1 /* mandatory */
#define HWSTUB_INFO_STMP 2 #define HWSTUB_INFO_STMP 2 /* optional */
#define HWSTUB_INFO_FEATURES 3 #define HWSTUB_INFO_FEATURES 3 /* mandatory */
#define HWSTUB_INFO_TARGET 4 /* mandatory */
struct usb_resp_info_version_t struct usb_resp_info_version_t
{ {
@ -89,14 +90,23 @@ struct usb_resp_info_stmp_t
#define HWSTUB_FEATURE_LOG (1 << 0) #define HWSTUB_FEATURE_LOG (1 << 0)
#define HWSTUB_FEATURE_MEM (1 << 1) #define HWSTUB_FEATURE_MEM (1 << 1)
#define HWSTUB_FEATURE_CALL (1 << 2) #define HWSTUB_FEATURE_CALL (1 << 2)
#define HWSTUB_FEATURE_JUMP (1 << 2) #define HWSTUB_FEATURE_JUMP (1 << 3)
#define HWSTUB_FEATURE_AES_OTP (1 << 3) #define HWSTUB_FEATURE_STOP (1 << 4)
struct usb_resp_info_features_t struct usb_resp_info_features_t
{ {
uint32_t feature_mask; uint32_t feature_mask;
}; };
#define HWSTUB_TARGET_UNK ('U' | 'N' << 8 | 'K' << 16 | ' ' << 24)
#define HWSTUB_TARGET_STMP ('S' | 'T' << 8 | 'M' << 16 | 'P' << 24)
struct usb_resp_info_target_t
{
uint32_t id;
char name[60];
};
/** /**
* HWSTUB_GET_LOG: only if has HWSTUB_FEATURE_LOG. * HWSTUB_GET_LOG: only if has HWSTUB_FEATURE_LOG.
* The log is returned as part of the control transfer. * The log is returned as part of the control transfer.
@ -119,11 +129,12 @@ struct usb_resp_info_features_t
* the transfer is either a read or a write. */ * the transfer is either a read or a write. */
/** /**
* HWSTUB_AES_OTP: only if has HWSTUB_FEATURE_AES_OTP. * HWSTUB_STOP: only if has HWSTUB_FEATURE_STOP.
* The control transfer contains the data to be en/decrypted and the data * Stop hwstub. Several methods can be employed (not all may be supported).
* is sent back on the interrupt endpoint. The first 16-bytes of the data * The method is stored in wValue and interpreted as follows:
* are interpreted as the IV. The output format is the same. * - reboot: immediately reboot the device
* The wValue field contains the parameters of the process. */ * - off: wait for USB disconnection and power off */
#define HWSTUB_AES_OTP_ENCRYPT (1 << 0) #define HWSTUB_STOP_REBOOT 0
#define HWSTUB_STOP_OFF 1
#endif /* __HWSTUB_PROTOCOL__ */ #endif /* __HWSTUB_PROTOCOL__ */

View file

@ -196,6 +196,11 @@ int target_get_info(int info, void **buffer)
*buffer = &g_stmp; *buffer = &g_stmp;
return sizeof(g_stmp); return sizeof(g_stmp);
} }
else if(info == HWSTUB_INFO_TARGET)
{
*buffer = &g_target;
return sizeof(g_target);
}
else else
return -1; return -1;
} }

View file

@ -41,6 +41,7 @@ struct hwstub_device_t g_hwdev;
struct usb_resp_info_version_t g_hwdev_ver; struct usb_resp_info_version_t g_hwdev_ver;
struct usb_resp_info_layout_t g_hwdev_layout; struct usb_resp_info_layout_t g_hwdev_layout;
struct usb_resp_info_features_t g_hwdev_features; struct usb_resp_info_features_t g_hwdev_features;
struct usb_resp_info_target_t g_hwdev_target;
struct usb_resp_info_stmp_t g_hwdev_stmp; struct usb_resp_info_stmp_t g_hwdev_stmp;
lua_State *g_lua; lua_State *g_lua;
@ -253,12 +254,22 @@ bool my_lua_import_hwstub()
lua_setfield(g_lua, -2, "ocram"); lua_setfield(g_lua, -2, "ocram");
lua_setfield(g_lua, -2, "layout"); lua_setfield(g_lua, -2, "layout");
lua_newtable(g_lua); // stmp lua_newtable(g_lua); // target
lua_pushinteger(g_lua, g_hwdev_stmp.chipid); lua_pushstring(g_lua, g_hwdev_target.name);
lua_setfield(g_lua, -2, "chipid"); lua_setfield(g_lua, -2, "name");
lua_pushinteger(g_lua, g_hwdev_stmp.rev); lua_pushinteger(g_lua, g_hwdev_target.id);
lua_setfield(g_lua, -2, "rev"); lua_setfield(g_lua, -2, "id");
lua_setfield(g_lua, -2, "stmp"); lua_setfield(g_lua, -2, "target");
if(g_hwdev_target.id == HWSTUB_TARGET_STMP)
{
lua_newtable(g_lua); // stmp
lua_pushinteger(g_lua, g_hwdev_stmp.chipid);
lua_setfield(g_lua, -2, "chipid");
lua_pushinteger(g_lua, g_hwdev_stmp.rev);
lua_setfield(g_lua, -2, "rev");
lua_setfield(g_lua, -2, "stmp");
}
lua_newtable(g_lua); // features lua_newtable(g_lua); // features
lua_pushboolean(g_lua, !!(g_hwdev_features.feature_mask & HWSTUB_FEATURE_LOG)); lua_pushboolean(g_lua, !!(g_hwdev_features.feature_mask & HWSTUB_FEATURE_LOG));
@ -269,8 +280,6 @@ bool my_lua_import_hwstub()
lua_setfield(g_lua, -2, "call"); lua_setfield(g_lua, -2, "call");
lua_pushboolean(g_lua, !!(g_hwdev_features.feature_mask & HWSTUB_FEATURE_JUMP)); lua_pushboolean(g_lua, !!(g_hwdev_features.feature_mask & HWSTUB_FEATURE_JUMP));
lua_setfield(g_lua, -2, "jump"); lua_setfield(g_lua, -2, "jump");
lua_pushboolean(g_lua, !!(g_hwdev_features.feature_mask & HWSTUB_FEATURE_AES_OTP));
lua_setfield(g_lua, -2, "aes_otp");
lua_setfield(g_lua, -2, "features"); lua_setfield(g_lua, -2, "features");
lua_pushlightuserdata(g_lua, (void *)&hw_read8); lua_pushlightuserdata(g_lua, (void *)&hw_read8);
@ -751,14 +760,25 @@ int main(int argc, char **argv)
goto Lerr; goto Lerr;
} }
// get STMP specific information // get target
ret = hwstub_get_info(&g_hwdev, HWSTUB_INFO_STMP, &g_hwdev_stmp, sizeof(g_hwdev_stmp)); ret = hwstub_get_info(&g_hwdev, HWSTUB_INFO_TARGET, &g_hwdev_target, sizeof(g_hwdev_target));
if(ret != sizeof(g_hwdev_stmp)) if(ret != sizeof(g_hwdev_target))
{ {
printf("Cannot get stmp: %d\n", ret); printf("Cannot get target: %d\n", ret);
goto Lerr; goto Lerr;
} }
// get STMP specific information
if(g_hwdev_target.id == HWSTUB_TARGET_STMP)
{
ret = hwstub_get_info(&g_hwdev, HWSTUB_INFO_STMP, &g_hwdev_stmp, sizeof(g_hwdev_stmp));
if(ret != sizeof(g_hwdev_stmp))
{
printf("Cannot get stmp: %d\n", ret);
goto Lerr;
}
}
// dump ROM // dump ROM
if(!g_quiet) if(!g_quiet)
{ {
@ -776,48 +796,6 @@ int main(int argc, char **argv)
fclose(f); fclose(f);
} }
// test DCP
#if 0
if(!g_quiet)
{
struct
{
uint8_t iv[16];
uint8_t data[16];
} __attribute__((packed)) dcp_test;
for(int i = 0; i < 16; i++)
dcp_test.iv[i] = rand();
for(int i = 0; i < 16; i++)
dcp_test.data[i] = rand();
printf("DCP\n");
printf(" IN\n");
printf(" IV:");
for(int i = 0; i < 16; i++)
printf(" %02x", dcp_test.iv[i]);
printf("\n");
printf(" IV:");
for(int i = 0; i < 16; i++)
printf(" %02x", dcp_test.data[i]);
printf("\n");
if(!hwstub_aes_otp(&g_hwdev, &dcp_test, sizeof(dcp_test), HWSTUB_AES_OTP_ENCRYPT))
{
printf(" OUT\n");
printf(" IV:");
for(int i = 0; i < 16; i++)
printf(" %02x", dcp_test.iv[i]);
printf("\n");
printf(" IV:");
for(int i = 0; i < 16; i++)
printf(" %02x", dcp_test.data[i]);
printf("\n");
}
else
printf("DCP error!\n");
}
#endif
/** Init lua */ /** Init lua */
// create lua state // create lua state

View file

@ -40,6 +40,7 @@ do
h:add("It contains some information about the device and the following methods."); h:add("It contains some information about the device and the following methods.");
h:add("* read8/16/32(a) reads a 8/16/32-bit integer at address a"); h:add("* read8/16/32(a) reads a 8/16/32-bit integer at address a");
h:add("* write8/16/32(a, v) writes the 8/16/32-bit integer v at address a"); h:add("* write8/16/32(a, v) writes the 8/16/32-bit integer v at address a");
h:add("* print_log() prints the device log");
h = HELP:create_topic("HW"); h = HELP:create_topic("HW");
h:add("This variable redirects to the current soc under hwstub.soc and should be changed by calling hwstub:soc:select only."); h:add("This variable redirects to the current soc under hwstub.soc and should be changed by calling hwstub:soc:select only.");
@ -70,6 +71,13 @@ if not hwstub.options.quiet then
print(" device") print(" device")
print(" version: " .. string.format("%d.%d.%d", hwstub.dev.version.major, print(" version: " .. string.format("%d.%d.%d", hwstub.dev.version.major,
hwstub.dev.version.minor, hwstub.dev.version.revision)) hwstub.dev.version.minor, hwstub.dev.version.revision))
print(" target")
local id_str = string.char(bit32.extract(hwstub.dev.target.id, 0, 8),
bit32.extract(hwstub.dev.target.id, 8, 8),
bit32.extract(hwstub.dev.target.id, 16, 8),
bit32.extract(hwstub.dev.target.id, 24, 8))
print(" id: " .. string.format("%#x (%s)", hwstub.dev.target.id, id_str))
print(" name: " .. hwstub.dev.target.name)
print(" layout") print(" layout")
print(" on-chip ram") print(" on-chip ram")
print(" code: " .. string.format("%#x bytes @ %#x", print(" code: " .. string.format("%#x bytes @ %#x",
@ -83,7 +91,6 @@ if not hwstub.options.quiet then
print(" mem: " .. tostring(hwstub.dev.features.mem)) print(" mem: " .. tostring(hwstub.dev.features.mem))
print(" call: " .. tostring(hwstub.dev.features.call)) print(" call: " .. tostring(hwstub.dev.features.call))
print(" jump: " .. tostring(hwstub.dev.features.jump)) print(" jump: " .. tostring(hwstub.dev.features.jump))
print(" aes_otp: " .. tostring(hwstub.dev.features.aes_otp))
end end
-- --