Skip to content

Instantly share code, notes, and snippets.

@carstene1ns
Created April 26, 2018 21:40
Show Gist options
  • Save carstene1ns/c6d3726d9f2a95620d4e4c83ec96355d to your computer and use it in GitHub Desktop.
Save carstene1ns/c6d3726d9f2a95620d4e4c83ec96355d to your computer and use it in GitHub Desktop.
Simple tool to get a romFS image out of a .NRO file
#!/usr/bin/env ruby
#
# dump_romfs.rb by carstene1ns, 2018 - under ISC License
#
# See http://switchbrew.org/index.php?title=NRO for reference
MAGIC = "NRO0"
HEADER = "ASET"
# helper
def error_out(msg)
puts "ERROR: #{msg}"
exit 1
end
# no args?
if ARGV.size != 1
puts "Usage: #{__FILE__} file.nro"
exit 0
end
# no file?
input = ARGV[0]
if not File.file?(input)
error_out "#{input} is not a file!"
end
# wrong type?
archive = open(input, 'rb')
archive.seek(16)
if archive.read(4) != MAGIC
error_out "#{input} is not a NRO file!"
end
# get size and jump to asset section
archive.seek(4, :CUR)
size = archive.read(4).unpack('L<')[0]
puts "Skipping to asset section (offset 0x%x)..." % size
archive.seek(size)
# size >= file size (no asset section)
if archive.eof? || archive.read(4) != HEADER
error_out "Did not find asset section!"
end
# format check
if archive.read(4).to_i != 0
error_out "Unknown asset format version!"
end
# get romfs
archive.seek(32, :CUR)
romfs_offset = archive.read(8).unpack('Q<')[0]
romfs_size = archive.read(8).unpack('Q<')[0]
if romfs_size == 0
puts "Romfs is empty :("
else
puts "Found romfs (%d bytes) at offset 0x%x," % [romfs_size, romfs_offset + size]
print "dumping to \"romfs.bin\"..."
archive.seek(romfs_offset + size)
output = open("romfs.bin", 'wb')
output.write(archive.read(romfs_size))
output.close
puts "done."
end
# cleanup
archive.close
exit 0
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment