Advertisement
Guest User

YvT Eboot Insert v1

a guest
May 22nd, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.05 KB | None | 0 0
  1. import binascii
  2. import struct
  3. import subprocess
  4. import pdb
  5.  
  6. #0x08A99E80 0x33a540
  7. TEXT_COL = 5
  8. POINTER_COL = 6
  9. del_space = str.maketrans({' ':None})
  10.  
  11. def text_decode(s):
  12.     while '{' in s:
  13.         pos = s.find('{')
  14.         endpos = s.find('}')
  15.         s1 = binascii.unhexlify(s[pos+1:endpos].translate(del_space)).decode('ascii')
  16.         s = s[:pos] + s1 + s[endpos+1:]
  17.     return s.encode('cp932')
  18. def update_pointer(filedata, addr, addr_orig, ptr):
  19. #Changes a pointer.
  20. #addr is where the text is
  21. #addr_orig is where the text was
  22. #ptr is where the pointer is
  23.     msb = True  #"Replace MSB" - most significant bits of pointer
  24.     lsb = True  #"Replace LSB"
  25.     addr1 = addr            #These are here for printing / debugging, they're not used
  26.     addr2 = addr_orig
  27.     if ptr[0] != '0':       #"Standard pointer" starts with 0
  28.         special_flag = True
  29.         if ptr[0] == '^':   #"Special pointer" msb only has "^" appended
  30.             lsb = False
  31.         elif ptr[0] == '&': #"Special pointer" lsb only has "&" appended
  32.             msb = False
  33.         else:
  34.             print(ptr)
  35.             print('error1', hex(addr_orig), ptr)
  36.             quit()
  37.         ptr = int(ptr[1:], 16)
  38.     else:
  39.         special_flag = False
  40.         ptr = int(ptr, 16)
  41.     if ptr < 0x8804000:     #Word pointer (easy)
  42.         filedata[ptr:ptr+4] = struct.pack('<I', addr - 0xC0)
  43.     else:                   #LI-style pointer, specified by virtual address
  44. #Some debugging stuff
  45. #        print(hex(addr_orig), hex(ptr), (addr + 0x8803F40) & 0xFFFF >= 0x8000,
  46. #              (addr_orig + 0x8803F40) & 0xFFFF >= 0x8000)
  47. #No idea what's going on here. Programming by guess and check
  48. #Those relocations are what's messing me up.
  49.         if (addr + 0x8803F40) & 0xFFFF >= 0x8000 and \
  50.            (addr_orig + 0x8803F40) & 0xFFFF >= 0x8000:
  51.             addr += 0x10000
  52.             addr_orig += 0x10000
  53.         elif (addr + 0x8803F40) & 0xFFFF < 0x8000 and \
  54.            (addr_orig + 0x8803F40) & 0xFFFF >= 0x8000:
  55.             addr_orig += 0x10000
  56.             addr += 0x10000
  57.         elif (addr + 0x8803F40) & 0xFFFF >= 0x8000 and \
  58.            (addr_orig + 0x8803F40) & 0xFFFF < 0x8000:
  59.             pass
  60.         ptr -= 0x8803F40                                #Convert to physical address                        
  61.         addr = struct.pack('<I', addr - 0xC0)           #Where the text will be
  62.         addr_orig = struct.pack('<I', addr_orig - 0xC0) #Where the text was
  63.         if msb == True:                                 #Replace MSB
  64.             msbaddr = ptr
  65.             filedata[ptr:ptr+2] = addr[2:]
  66.         if lsb == True:                                 #Replace LSB
  67.             if not special_flag:
  68.                 while True:
  69.                     searchedfor = addr_orig[:2]         #For debugging, not used
  70.                     #Search for the lsb part of the pointer
  71.                     ptr = filedata.find(addr_orig[:2], ptr + 1)    
  72.                     if ptr == -1:   #Not found
  73.                         print('error2', binascii.hexlify(addr_orig), binascii.hexlify(searchedfor))
  74.                         quit()
  75.                     #Make sure this is really it. Looking for addiu opcode.
  76. #                    print('hit', ptr % 4 == 0, filedata[ptr+3] >> 4 == 2)
  77.                     if ptr % 4 == 0:
  78.                         break
  79.             lsbaddr = ptr                               #For debugging, not used
  80.             filedata[ptr:ptr+2] = addr[:2]              #Replace LSB
  81.         if msb and lsb:
  82.             if lsbaddr - msbaddr > 0xC:
  83.                 print(hex(addr1+0x8803f40), hex(addr2), hex(msbaddr + 0x8803F40), hex(lsbaddr + 0x8803F40),
  84.                       binascii.hexlify(searchedfor))
  85.     return filedata        
  86.  
  87. def CSVfix(filedata):
  88.     start = 0x0891B458 - 0x8803F40
  89.     end = 0x0891C010 - 0x8803F40
  90.     s = binascii.unhexlify('2C000224')
  91.     s1 = binascii.unhexlify('1F000224')
  92.     pos = start
  93.     while pos < end:
  94.         if filedata[pos:pos+4] == s:
  95.             filedata[pos:pos+4] = s1
  96.         pos += 4
  97.     return filedata
  98.  
  99. with open('EBOOTBASE.bin', 'rb') as f:  #Has extra program header and 0x2000 bytes at the end
  100.     filedata = bytearray(f.read())
  101. filedata = CSVfix(filedata)
  102. with open('data1.tsv', 'r', encoding='utf-8') as f:     #Read data
  103.     inputdata = []
  104.     for line in f:
  105.         line = line.rstrip('\r\n').split('\t')
  106.         if line[0] == '':
  107.             inputdata[-1][1].append(text_decode(line[TEXT_COL]))
  108.         else:
  109.             line[0] = int(line[0], 16)
  110.             col = POINTER_COL
  111.             pointers = []
  112.             while line[col] != '':
  113.                 pointers.append(line[col])
  114.                 col += 1
  115.                 if col == len(line):
  116.                     break
  117.             inputdata.append([line[0], [text_decode(line[TEXT_COL])], pointers])
  118. inserted_strings = []                           #Keep track of inserted strings
  119. nextpos = 0x33A540                              #Keep track of insert point within the new 0x2000 segment
  120. for addr, lines, pointers in sorted(inputdata, key=lambda x: x[0], reverse=True):
  121.     addr_orig = addr                            #Needed to call update_pointer
  122.     avail_len = filedata.find(b'\x00', addr)
  123.     while filedata[avail_len] == 0:
  124.         avail_len += 1
  125.     avail_len = avail_len - addr - 1
  126. ##    print(avail_len)
  127.     s = b'\n'.join(lines)                       #Get the string to insert
  128. ##    if s in [x[1] for x in inserted_strings]:   #Already there
  129. ####        print(hex(addr))
  130. ##        filedata[addr:addr+avail_len] = b'\x00' * avail_len #Blank out the string
  131. ##        for addr, s1 in inserted_strings:                   #Find where the string is
  132. ##            if s == s1:
  133. ##                break
  134. ##        else:
  135. ##            print('error3')
  136. ##            quit()
  137. ##        for ptr in pointers:                                #Update each pointer
  138. ##            filedata = update_pointer(filedata, addr, addr_orig, ptr)
  139. ##        continue
  140.     inserted_strings.append([addr, s])                      #Add the string and position to the list
  141.     if len(s) > avail_len:                                  #Not enough space
  142.         filedata[addr:addr+avail_len] = b'\x00' * avail_len #Blank out the string
  143.         inserted_strings[-1][0] = nextpos - 0xA46C0 + 0xC0  #Update inserted_strings with new position
  144.         s += b'\x00' * (4 - (len(s) % 4))                   #Pad out string with nulls
  145.         filedata[nextpos:nextpos+len(s)] = s                #write the string
  146.         for ptr in pointers:                                #update each pointer
  147.             filedata = update_pointer(filedata, nextpos - 0xA46C0 + 0xC0, addr_orig, ptr)
  148.         nextpos += len(s)                                   #update string insert position
  149.     else:                                                   #enough space
  150.         filedata[addr:addr+avail_len] = b'\x00' * avail_len #Blank out the string
  151.         filedata[addr:addr+len(s)] = s                      #Replace the string
  152. with open('EBOOT.BIN', 'wb') as f:
  153.     f.write(filedata)
  154. subprocess.run(['armipsd', 'YsVsSora.asm'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement