Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Insert Nayuta eboot.
- # Need decrypted eboot in file boot.orig and ebootdump.csv w/ text to insert
- # ebootdump.csv: must be in tab-separated format. Cols beyond the 2nd are ignored
- # ebootdump.csv: must be saved with SHIFT-JIS encoding
- import os
- import struct
- def get_data(filename):
- totalbytes = os.path.getsize(filename)
- infile = open(filename, 'rb')
- totalfiledata = infile.read(totalbytes)
- return totalfiledata
- def replacestr(origstr,replacestr,startpos,replacelen):
- #Returns a string with a replaced sub-string
- #origstr - the original string, replacestr = the string to replace
- #startpos - where the replacement string should go
- #replacelen - how many characters of the original string to replace
- return origstr[:startpos] + replacestr + origstr[startpos+replacelen:]
- def text_decode(text):
- #Changes {} entries in source file into hex
- while '{ ' in text:
- startpos = text.find('{ ')
- endpos = text.find(' }')
- s = text[startpos+1:endpos]
- s = s.translate(None,' ')
- s = s.decode('hex')
- text = text[:startpos] + s + text[endpos+2:]
- return text
- #Read input data
- inputdata = []
- with open('ebootdump.csv','rb') as f:
- for line in f:
- line = line.translate(None,"\r\n") #Trims newline characters
- line = line.split('\t')
- line[0] = int(line[0],16)
- if line[2] != '':
- line[2] = int(line[2],16)
- if line[3] != '':
- line[3] = int(line[3])
- inputdata.append(line)
- filedata = get_data('NPJH50625.BIN')
- addr_mov = 0x221FCC #First address to put moved stuff
- flag = False
- flag2 = False
- #Goes in reverse order to make room for strings
- for addr, text, pointer, pointertype in reversed(inputdata):
- pos = filedata.find('\x00',addr) #Find end of string
- while filedata[pos] == '\x00': #Find available length for string
- pos += 1
- avail_len = pos - addr - 1
- text = text_decode(text) #Get the string to insert
- if len(text) > avail_len: #If the string won't fit...
- if pointer == '' or pointertype == '':
- print 'No pointer available!'
- if pointertype == 1: #Type 1 pointer: Address in EBOOT
- print 'Attempting to update entry %s - Pointer type 1.' % hex(addr)
- #Zero out the original entry
- filedata = replacestr(filedata,'\00'*avail_len,addr,avail_len)
- #Update the pointer
- filedata = replacestr(filedata,struct.pack('<I',addr_mov-0xc0),
- pointer,4)
- #Put the string where moved stuff goes
- filedata = replacestr(filedata,text,addr_mov,len(text))
- #Compute new addr for moved stuff (word aligned)
- addr_mov = addr_mov + len(text) + 1
- while addr_mov % 4 != 0:
- addr_mov += 1
- #If the first block for moved stuff is exhausted, jump to 2nd block
- if flag == False and addr_mov > 0x2220b0:
- flag = True
- addr_mov = 0x223f0c #Address for 2nd block
- #If the second block for moved stuff is exhausted, jump to 3rd block
- if flag2 == False and addr_mov > 0x223ff8:
- flag2 = True
- addr_mov = 0x224d18 #Address for 3rd block
- elif pointertype in (2,3): #Type 2/3 pointers are LI instructions
- #Type 2 is standard LI. Type 3 is split LI where there is a
- #one instruction delay between the halves
- print 'Attempting to update entry %s - Pointer type %d.' % \
- (hex(addr),pointertype)
- #Zero out the original entry
- filedata = replacestr(filedata,'\00'*avail_len,addr,avail_len)
- #Compute the EBOOT pointer address
- pointer = pointer - 0x8803f40
- #Replace the high-order bits of the target address
- filedata = replacestr(filedata,struct.pack('<I',addr_mov-0xc0)[2:4],
- pointer,2)
- #Move the pointer to the low-order bits of the target address
- if pointertype == 2:
- pointer += 4
- else:
- pointer += 8
- #Replace the low-order bits of the target address
- filedata = replacestr(filedata,struct.pack('<I',addr_mov-0xc0)[0:2],
- pointer,2)
- #Put the new string where moved stuff goes
- filedata = replacestr(filedata,text,addr_mov,len(text))
- #Compute new addr for moved stuff (word aligned)
- addr_mov = addr_mov + len(text) + 1
- while addr_mov % 4 != 0:
- addr_mov += 1
- if flag == False and addr_mov > 0x2220b0:
- flag = True
- addr_mov = 0x223f0c
- if flag2 == False and addr_mov > 0x223ff8:
- flag2 = True
- addr_mov = 0x224d18
- else:
- #If we don't know the pointer, all we can do is truncate to fit
- print "Text %s at address %s len is %d; max len is %d" \
- % (text, hex(addr), len(text), avail_len)
- text = text[:avail_len]
- text = text + (avail_len - len(text))*'\x00'
- filedata = replacestr(filedata,text,addr,avail_len)
- else: #This block is for if the string will fit in the available space
- text = text + (avail_len - len(text))*'\x00'
- filedata = replacestr(filedata,text,addr,avail_len)
- #Write output
- outfile = open('EBOOT.BIN','wb')
- outfile.write(filedata)
- outfile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement