]> git.zerfleddert.de Git - proxmark3-svn/blame - client/scripting.c
Added binlib, to handle binary data from lua, based on lpack http://www.tecgraf.puc...
[proxmark3-svn] / client / scripting.c
CommitLineData
5b760b6c 1//-----------------------------------------------------------------------------
2// Copyright (C) 2013 m h swende <martin at swende.se>
3//
4// This code is licensed to you under the terms of the GNU GPL, version 2 or,
5// at your option, any later version. See the LICENSE.txt file for the text of
6// the license.
7//-----------------------------------------------------------------------------
8// Some lua scripting glue to proxmark core.
9//-----------------------------------------------------------------------------
10
11#include <lua.h>
12#include <lualib.h>
13#include <lauxlib.h>
14#include "proxmark3.h"
15#include "usb_cmd.h"
16#include "cmdmain.h"
17#include "scripting.h"
18/**
19 * The following params expected:
20 * UsbCommand c
21 *@brief l_SendCommand
22 * @param L
23 * @return
24 */
25static int l_SendCommand(lua_State *L){
26
27 /*
28 *
29 The SendCommand (native) expects the following structure:
30
31 typedef struct {
32 uint64_t cmd; //8 bytes
33 uint64_t arg[3]; // 8*3 bytes = 24 bytes
34 union {
35 uint8_t asBytes[USB_CMD_DATA_SIZE]; // 1 byte * 512 = 512 bytes (OR)
36 uint32_t asDwords[USB_CMD_DATA_SIZE/4]; // 4 byte * 128 = 512 bytes
37 } d;
38 } PACKED UsbCommand;
39
40 ==> A 544 byte buffer will do.
41 **/
42 //Pop cmd
96e7a3a5 43 size_t size;
44 const char *data = luaL_checklstring(L, 1, &size);
45 if(size != sizeof(UsbCommand))
46 {
f057bddb 47 printf("Got data size %d, expected %d" , (int) size,(int) sizeof(UsbCommand));
96e7a3a5 48 lua_pushstring(L,"Wrong data size");
49 return 1;
50 }
51
52// UsbCommand c = (*data);
53 SendCommand(data);
5b760b6c 54 return 0;
96e7a3a5 55 //UsbCommand *c = (UsbCommand *)lua_touserdata(L, 1);
56 //luaL_argcheck(L, c != NULL, 1, "'UsbCommand' expected");
57
58 //SendCommand(c);
59 //return 0;
5b760b6c 60}
61/**
62 * @brief The following params expected:
63 * uint32_t cmd
64 * size_t ms_timeout
65 * @param L
66 * @return
67 */
68static int l_WaitForResponseTimeout(lua_State *L){
69
70 //pop cmd
71 uint32_t cmd = luaL_checkunsigned(L,1);
72 printf("in l_WaitForResponseTimeout, got cmd 0x%0x\n",(int) cmd);
73 //UsbCommand response;
74
96e7a3a5 75 //We allocate the usbcommand as userdata on the Lua-stack
5b760b6c 76 size_t nbytes = sizeof(UsbCommand);
77
78 UsbCommand *response = (UsbCommand *)lua_newuserdata(L, nbytes);
79
80 size_t ms_timeout = 2000;
81 //Did the user send a timeout ?
82 //Check if the current top of stack is an integer
83
84 if(lua_isnumber( L, 2))
85 {
86 printf("You sent a timout-value\n");
87 ms_timeout = luaL_checkunsigned(L,2);
88 }
89 printf("Timeout set to %dms\n" , (int) ms_timeout);
90
91 if(WaitForResponseTimeout(cmd, response, ms_timeout))
92 {
93 //Return the UsbCommand as userdata
94 //the usbcommand is already on the stack
95 // return 1 to signal one return value
96 return 1;
97 }else
98 {
99 //Don't return the UsbCommand. Pop it.
100 lua_pop(L,-1);
101 //Push a Nil instead
102 lua_pushnil(L);
103 return 1;
104 }
105}
106static int l_nonce2key(lua_State *L){ return CmdHF14AMfRdSc(luaL_checkstring(L, 1));}
107static int l_PrintAndLog(lua_State *L){ return CmdHF14AMfDump(luaL_checkstring(L, 1));}
108
f057bddb 109int set_pm3_libraries(lua_State *L)
5b760b6c 110{
111 static const luaL_Reg libs[] = {
112 {"SendCommand", l_SendCommand},
113 {"WaitForResponseTimeout", l_WaitForResponseTimeout},
114 {"nonce2key", l_nonce2key},
115 {"PrintAndLog", l_PrintAndLog},
116 {NULL, NULL}
117 };
118
119 lua_pushglobaltable(L);
120 // Core library is in this table. Contains '
121 //this is 'pm3' table
122 lua_newtable(L);
123
124 //Put the function into the hash table.
125 for (int i = 0; libs[i].name; i++) {
126 lua_pushcfunction(L, libs[i].func);
127 lua_setfield(L, -2, libs[i].name);//set the name, pop stack
128 }
129 //Name of 'core'
130 lua_setfield(L, -2, "core");
131
132 //-- remove the global environment table from the stack
133 lua_pop(L, 1);
134 return 1;
135}
Impressum, Datenschutz