You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.8 KiB

12 months ago
# -*- encoding: utf-8 -*-
"""
@File : read_bin.py
@License : (C)Copyright 2021-2023
@Modify Time @Author @Version @Description
------------ ------- -------- -----------
2023/10/9 16:27 zart20 1.0 None
"""
# SECTOR_SIZE = 512 # 扇区大小
# BYTES_PER_LINE = 16 # 每行字节数
# # MAX_SECTORS_TO_READ = 15927 # 要读取的最大扇区数量
# MAX_SECTORS_TO_READ = 0
# # 打开设备文件
# # drive_letter = "I:" # 请替换为你要操作的磁盘
file_path = f"output.bin" # 设备文件路径
#
# try:
# with open(file_path, 'rb') as disk:
# sector_number = 0 # 起始扇区号
# byte_offset = 0 # 字节偏移量初始化
# sectors_read = 0 # 已读取的扇区数量
#
# while sectors_read <= MAX_SECTORS_TO_READ:
# # 读取扇区数据
# disk.seek(sector_number * SECTOR_SIZE)
# sector_data = disk.read(SECTOR_SIZE)
#
# # 输出扇区数据
# print(f"扇区号:{sector_number}")
# for i in range(0, len(sector_data), BYTES_PER_LINE):
# line_data = sector_data[i:i + BYTES_PER_LINE]
# print(f"{byte_offset + i:08X}:", end=" ") # 输出字节偏移量16进制
# for byte in line_data:
# print(f"{byte:02X}", end=" ") # 输出每个字节的16进制表示
#
# print() # 换行
#
# # 更新扇区号,读取下一个扇区
# sector_number += 1
# byte_offset += len(sector_data) # 累加字节偏移量
# sectors_read += 1
#
# except PermissionError:
# print("没有足够的权限来访问磁盘。请以管理员身份运行程序。")
# except FileNotFoundError:
# print(f"找不到设备文件:{file_path}")
# except Exception as e:
# print(f"发生错误:{e}")
def read_sector_0():
SECTOR_SIZE = 512 # 扇区大小
BYTES_PER_LINE = 16 # 每行字节数
SECTORS = 0
with open(file_path, 'rb') as disk:
disk.seek(SECTORS *SECTOR_SIZE)
sector_data = disk.read(SECTOR_SIZE)
# for i in range(0, len(sector_data), BYTES_PER_LINE):
# line_data=sector_data[i:i+BYTES_PER_LINE]
# print(f"{SECTORS*32+i:08X}:", end=" ")
# for byte in line_data:
# print(f"{byte:02X}", end=" ")
# disk_dict[f"{i:08X}:"].append(f"{byte:02X}")
# print()
return SECTORS, sector_data
def read_sector_FAT1():
SECTOR_SIZE = 512 # 扇区大小
BYTES_PER_LINE = 16 # 每行字节数
SECTORS = 34
with open(file_path, 'rb') as disk:
disk.seek(SECTORS *SECTOR_SIZE)
sector_data = disk.read(SECTOR_SIZE)
# for i in range(0, len(sector_data), BYTES_PER_LINE):
# line_data=sector_data[i:i+BYTES_PER_LINE]
# print(f"{SECTORS*32+i:08X}:", end=" ")
# for byte in line_data:
# print(f"{byte:02X}", end=" ")
# disk_dict[f"{i:08X}:"].append(f"{byte:02X}")
# print()
return SECTORS, sector_data
def read_sector_root_path(num):
SECTOR_SIZE = 512 # 扇区大小
BYTES_PER_LINE = 16 # 每行字节数
SECTORS = 15032+num
with open(file_path, 'rb') as disk:
disk.seek(SECTORS *SECTOR_SIZE)
sector_data = disk.read(SECTOR_SIZE)
# for i in range(0, len(sector_data), BYTES_PER_LINE):
# line_data=sector_data[i:i+BYTES_PER_LINE]
# print(f"{SECTORS*32+i:08X}:", end=" ")
# for byte in line_data:
# print(f"{byte:02X}", end=" ")
# print(f"{chr(byte)}", end="")
# print()
# print(sector_data)
return SECTORS, sector_data
if __name__ == '__main__':
read_sector_0()
read_sector_FAT1()
read_sector_root_path(0)