From dd6b8427f9b4e3d1a13ed81005f9020394001d0c Mon Sep 17 00:00:00 2001 From: Amaury Pouly Date: Sun, 12 Nov 2017 14:13:21 +0100 Subject: hwstub: expose read/write functions Previously only atomic read/write 8/16/32 were exposed. But it is useful to be able to read a whole buffer at once, this is more efficient than N times read8. Change-Id: I06e331641e1ab1f74c0e16e8c432eafb398e8e6d --- utils/hwstub/tools/hwstub_shell.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/utils/hwstub/tools/hwstub_shell.cpp b/utils/hwstub/tools/hwstub_shell.cpp index a852e0f515..568bb37f9f 100644 --- a/utils/hwstub/tools/hwstub_shell.cpp +++ b/utils/hwstub/tools/hwstub_shell.cpp @@ -253,6 +253,21 @@ soc_word_t hw_read32(lua_State *state, soc_addr_t addr) return u; } +uint8_t *hw_read(lua_State *state, soc_addr_t addr, soc_word_t size) +{ + uint8_t *buf = new uint8_t[size]; + size_t sz = size; + error ret = g_hwdev->read(addr, buf, sz, false); + if(ret != error::SUCCESS || sz != size) + { + delete[] buf; + luaL_error(state, "fail to read<%d> @ %p: %d", size, addr, ret); + } + if(g_print_mem_rw) + printf("[read<%lu> @ %#lx = ...]\n", (unsigned long)size, (unsigned long)addr); + return buf; +} + void hw_write8(lua_State *state, soc_addr_t addr, soc_word_t val) { uint8_t u = val; @@ -296,6 +311,19 @@ int my_lua_readn(lua_State *state) return 1; } +int my_lua_read(lua_State *state) +{ + int n = lua_gettop(state); + if(n != 2) + luaL_error(state, "read takes a two arguments: address, length"); + unsigned length = mylua_checkunsigned(state, 2); + uint8_t *buf = hw_read(state, mylua_checkunsigned(state, 1), length); + /* lua strings are really byte buffers, they can contain arbitrary bytes including zeroes */ + lua_pushlstring(state, (const char *)buf, length); + delete[] buf; + return 1; +} + int my_lua_writen(lua_State *state) { hw_writen_fn_t fn = (hw_writen_fn_t)lua_touserdata(state, lua_upvalueindex(1)); @@ -797,6 +825,8 @@ bool my_lua_import_hwstub() lua_pushlightuserdata(g_lua, (void *)&hw_read32); lua_pushcclosure(g_lua, my_lua_readn, 1); lua_setfield(g_lua, -2, "read32"); + lua_pushcclosure(g_lua, my_lua_read, 0); + lua_setfield(g_lua, -2, "read"); lua_pushlightuserdata(g_lua, (void *)&hw_write8); lua_pushcclosure(g_lua, my_lua_writen, 1); -- cgit v1.2.3