extract_debugfs.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # This file is part of asmc, a bootstrapping OS with minimal seed
  4. # Copyright (C) 2018 Giovanni Mascellani <gio@debian.org>
  5. # https://gitlab.com/giomasce/asmc
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. import sys
  17. import os
  18. import struct
  19. def main():
  20. assert len(sys.argv) == 2
  21. disk_filename = sys.argv[1]
  22. with open(disk_filename, 'rb') as fdisk:
  23. mbr = fdisk.read(512)
  24. assert len(mbr) == 512
  25. parts_data = mbr[0x1be:0x1fe]
  26. for i in range(4):
  27. part_data = parts_data[16*i:16*(i+1)]
  28. if part_data[4] == b'\x00':
  29. continue
  30. start_lba, sectors = struct.unpack('<II', part_data[8:])
  31. fdisk.seek(start_lba * 512, os.SEEK_SET)
  32. magic = fdisk.read(8)
  33. if magic == b'DEBUGFS ':
  34. print('Debug partition found!')
  35. current_lba = start_lba + 1
  36. fdisk.seek(current_lba * 512, os.SEEK_SET)
  37. while True:
  38. filename_raw = fdisk.read(512)
  39. current_lba += 1
  40. filename = bytes([b for b in filename_raw if b != 0]).decode('ascii')
  41. if filename == '':
  42. break
  43. with open('debugfs/{}'.format(filename), 'wb') as fout:
  44. while True:
  45. block = fdisk.read(512)
  46. current_lba += 1
  47. if block == b'\x00' * 512:
  48. break
  49. fout.write(bytes([b for b in block if b != 0]))
  50. print(filename)
  51. if __name__ == '__main__':
  52. main()