Skip to content

Commit 0c69851

Browse files
Merge branch 'master' of gitlab.amer.irdeto.com:ben.gardiner/rhme3
2 parents 6b4955b + 11f093f commit 0c69851

File tree

2 files changed

+91
-4
lines changed

2 files changed

+91
-4
lines changed

atxmega128a4u/scripts/avr_stack_vars.py

+55-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,17 @@ def make_stack_variable(func_start, offset, name, size):
4242
None, size) == 0:
4343
return 1
4444
else:
45-
return 0
45+
raise ValueError("failed to create stack frame member %s @ +0x%x in function @ 0x%x" % (name, offset, func_start))
46+
47+
def get_stack_variable_name(func_start, offset):
48+
func = idaapi.get_func(func_start)
49+
frame = idaapi.get_frame(func)
50+
if frame is None:
51+
raise ValueError("couldn't get frame for function @ 0x%x" % func_start)
52+
53+
offset += func.frsize
54+
member = idaapi.get_member(frame, offset)
55+
return idaapi.get_member_name(member.id)
4656

4757
def is_latter_of_stack_sequential_instructions(prev_line, curr_line, op_num):
4858
other_op_num = 0
@@ -74,6 +84,9 @@ def operand_to_stack_variable(curr_line, op_num):
7484
idc.OpStkvar(curr_line.ea, op_num)
7585
return
7686

87+
def get_next_line(line):
88+
return sark.Line(line.ea + len(line.bytes))
89+
7790
def create_stack_variable_from_operand(curr_line, op_num):
7891
try:
7992
curr_insn = curr_line.insn
@@ -86,14 +99,19 @@ def create_stack_variable_from_operand(curr_line, op_num):
8699
stack_offset = curr_insn.operands[op_num].offset
87100
this_function = sark.Function(curr_line.ea)
88101

89-
next_line = sark.Line(curr_line.ea + len(curr_line.bytes))
102+
next_line = get_next_line(curr_line)
90103
size = 2 if is_latter_of_stack_sequential_instructions(curr_line, next_line, op_num) else 1
91104

92105
logger.info("creating %d byte stack variable @ 0x%x based on %s && %s" % (size, stack_offset, curr_line, next_line))
93106

94107
make_stack_variable(this_function.startEA, stack_offset, "var_%x" % stack_offset, size)
95108
return
96109

110+
def is_sensible_instruction(insn):
111+
return (len(insn.operands) == 2 and
112+
str(insn.operands[0]) != '' and str(insn.operands[1]) != ''
113+
)
114+
97115
def all_y_stack_vars_here():
98116
ea = idc.here()
99117
this_function = sark.Function(ea)
@@ -131,9 +149,43 @@ def all_y_stack_vars_here():
131149
if not is_latter_of_stack_sequential_instructions(prev, line, 0): # avoid marking a stack var for the second part of a sequential load
132150
create_stack_variable_from_operand(line, 0)
133151

134-
# TODO also for Y+ in operand 0
135152
prev = line
136153

154+
for a1st_line in this_function.lines:
155+
a2nd_line = get_next_line(a1st_line)
156+
a3rd_line = get_next_line(a2nd_line)
157+
158+
if a2nd_line.ea > this_function.endEA or a3rd_line.ea > this_function.endEA:
159+
continue
160+
161+
try:
162+
a1st_insn = a1st_line.insn
163+
a2nd_insn = a2nd_line.insn
164+
a3rd_insn = a3rd_line.insn
165+
except sark.exceptions.SarkNoInstruction:
166+
logger.debug("skipping %s && %s && %s" % (a1st_line, a2nd_line, a3rd_line))
167+
continue
168+
169+
if (not is_sensible_instruction(a1st_insn)) or (not is_sensible_instruction(a2nd_insn)) or (not is_sensible_instruction(a3rd_insn)):
170+
logger.debug("filtering %s && %s && %s" % (a1st_insn, a2nd_insn, a3rd_insn))
171+
continue
172+
173+
if (str(a1st_insn.mnem) == 'movw' and (str(a1st_insn.operands[1]) == 'YL' or str(a1st_insn.operands[1].reg) == 'r28') and
174+
str(a1st_insn.operands[0]) == str(a2nd_insn.operands[0]) and a2nd_insn.operands[0].reg == avr_get_register_pairs().get(a3rd_insn.operands[0].reg) and
175+
str(a2nd_insn.mnem) == 'subi' and str(a3rd_insn.mnem) == 'sbci'
176+
):
177+
stack_offset = -1 * int(a2nd_insn.operands[1].text, 0)
178+
this_function = sark.Function(a1st_line.ea)
179+
180+
# TODO: guess size of stack var
181+
size = 1
182+
logger.info("creating %d byte stack variable @ 0x%x based on %s && %s && %s" % (size, stack_offset, a1st_line, a2nd_line, a3rd_line))
183+
184+
make_stack_variable(this_function.startEA, stack_offset, "var_%x" % stack_offset, size)
185+
186+
name = get_stack_variable_name(this_function.startEA, stack_offset)
187+
idc.OpAlt(a2nd_line.ea, 1, name)
188+
137189
print("some utility functions are defined:\nall_y_stack_vars_here()")
138190

139191
except:

unauthorized/notes.md

+36-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,42 @@ parse_and_maybe_set_flag_printer(input){
149149
if (saved_position != 0x1f)
150150
die_and_remember
151151

152-
152+
TODO
153+
154+
## `ROM:046D get_valid_rand`
155+
156+
This function is called by the `parse_and_maybe_set_flag_printer` function above. At this point I couldn't handle keeping track of `Y+NN` anymore so I wrote a stack-variable making script basing Y as the stack pointer (which avr-gcc appears to use).
157+
158+
maybe_test_const_rng(illegal_rand) {
159+
word last_rand;
160+
for (i=0; i<= 0xff; i++)
161+
last_rand = prob_get_rand();
162+
if (last_rand == illegal_rand) {
163+
illegal_rand = last_rand;
164+
for (i=0; i<0x400; i++) {
165+
last_rand = prob_get_rand();
166+
}
167+
if (last_rand == illegal_rand)
168+
die();
169+
}
170+
if (illegal_rand > 0x21 ) {
171+
illegal_rand =- 0x30;
172+
illegal_rand[H] = -1 * (illegal_rand[L] << 1 - illegal_rand[L] << 1);
173+
illegal_rand[L] = illegal_rand[L] << 2;
174+
}
175+
illegal_rand_copy = illegal_rand;
176+
j=0;
177+
while(j < illegal_rand && illegal_rand_copy != 0) {
178+
busy_mux();
179+
j++;
180+
busy_mux();
181+
illegal_rand_copy--;
182+
}
183+
if (j != illegal_rand || illegal_rand_copy == 0)
184+
die_and_remember();
185+
busy_mux()
186+
return illegal_rand_copy;
187+
}
153188

154189

155190

0 commit comments

Comments
 (0)