[read_info] Fix detection active rootfs partition

pull/24/head
remittor 2 years ago
parent 3ef21ae07f
commit 7c37aaf73a

@ -52,6 +52,7 @@ class DevInfo():
dmesg = None # text dmesg = None # text
info = BaseInfo() info = BaseInfo()
partlist = [] # list of {addr, size, name} partlist = [] # list of {addr, size, name}
kcmdline_s = "" # original kernel command line
kcmdline = {} # key=value kcmdline = {} # key=value
nvram = {} # key=value nvram = {} # key=value
rootfs = RootFS() rootfs = RootFS()
@ -80,12 +81,8 @@ class DevInfo():
self.get_part_table() self.get_part_table()
if not self.partlist or len(self.partlist) <= 1: if not self.partlist or len(self.partlist) <= 1:
die("Partition list is empty!") die("Partition list is empty!")
self.get_rootfs()
self.get_kernel_cmdline() self.get_kernel_cmdline()
if self.rootfs.num is None: self.get_rootfs()
if 'firmware' in self.kcmdline:
self.rootfs.num = int(self.kcmdline['firmware'])
print(f'rootfs.num = {self.rootfs.num}\n')
self.get_baseinfo() self.get_baseinfo()
if not self.info.cpu_arch: if not self.info.cpu_arch:
die("Can't detect CPU arch! Try to reboot device.") die("Can't detect CPU arch! Try to reboot device.")
@ -243,39 +240,42 @@ class DevInfo():
def get_rootfs(self, verbose = None): def get_rootfs(self, verbose = None):
verbose = verbose if verbose is not None else self.verbose verbose = verbose if verbose is not None else self.verbose
self.rootfs = RootFS() self.rootfs = RootFS()
if not self.dmesg: if not self.kcmdline_s and not self.dmesg:
return self.rootfs return self.rootfs
# flag_boot_rootfs=0 mounting /dev/mtd10 kcmdline = f'Kernel command line: {self.kcmdline_s} \n'
x = re.search(r'flag_boot_rootfs=([0-9]) mounting (\S+)', self.dmesg) if self.dmesg:
if x: # flag_boot_rootfs=0 mounting /dev/mtd10
self.rootfs.num = int(x.group(1)) x = re.search(r'flag_boot_rootfs=([0-9]) mounting (\S+)', self.dmesg)
self.rootfs.mtd_dev = x.group(2) if x:
# UBI: attached mtd10 (name "rootfs0", size 32 MiB) to ubi0 self.rootfs.num = int(x.group(1))
x = re.search(r'attached mtd([0-9]+) \(name "(.*?)", size', self.dmesg) self.rootfs.mtd_dev = x.group(2)
if x and x.group(2).lower().startswith('rootfs'): # UBI: attached mtd10 (name "rootfs0", size 32 MiB) to ubi0
self.rootfs.mtd_num = int(x.group(1)) x = re.search(r'attached mtd([0-9]+) \(name "(.*?)", size', self.dmesg)
self.rootfs.partition = x.group(2).strip() if x and x.group(2).lower().startswith('rootfs'):
# mtd: device 11 (rootfs) set to be root filesystem self.rootfs.mtd_num = int(x.group(1))
x = re.search(r'mtd: device ([0-9]+) \(rootfs\) set to be root filesystem', self.dmesg) self.rootfs.partition = x.group(2).strip()
if x: # mtd: device 11 (rootfs) set to be root filesystem
self.rootfs.mtd_num = int(x.group(1)) x = re.search(r'mtd: device ([0-9]+) \(rootfs\) set to be root filesystem', self.dmesg)
if x:
self.rootfs.mtd_num = int(x.group(1))
if self.rootfs.num is None: if self.rootfs.num is None:
k = re.search(r'Kernel command line:(.*?) ubi\.mtd=(\S+)', self.dmesg) # ([^\s]+) k = re.search(r'Kernel command line:(.*?) ubi\.mtd=(\S+)', kcmdline) # ([^\s]+)
if k: if k:
self.rootfs.partition = k.group(2) self.rootfs.partition = k.group(2)
if self.rootfs.num is None: if self.rootfs.num is None:
k = re.search(r'Kernel command line:(.*?) firmware=([0-9])', self.dmesg) k = re.search(r'Kernel command line:(.*?) firmware=([0-9])', kcmdline)
if k: if k:
self.rootfs.num = int(k.group(2)) self.rootfs.num = int(k.group(2))
if self.rootfs.num is None and self.rootfs.mtd_num is None: if self.rootfs.num is None and self.rootfs.mtd_num is None:
x = re.search(r'Kernel command line:(.*?) root=(\S+)', self.dmesg) x = re.search(r'Kernel command line:(.*?) root=(\S+)', kcmdline)
if x and x.group(2).startswith('/dev/mtdblock'): if x and x.group(2).startswith('/dev/mtdblock'):
self.rootfs.mtd_dev = x.group(2) self.rootfs.mtd_dev = x.group(2)
self.rootfs.mtd_num = int(self.rootfs.mtd_dev.replace('/dev/mtdblock', '')) self.rootfs.mtd_num = int(self.rootfs.mtd_dev.replace('/dev/mtdblock', ''))
if self.rootfs.num is None and self.rootfs.partition: if self.rootfs.num is None and self.rootfs.partition:
if self.rootfs.partition.lower().startswith('rootfs'): pname = self.rootfs.partition.lower()
if pname.startswith('rootfs') or pname.startswith('firmware') or pname.startswith('ubi'):
self.rootfs.num = 0 self.rootfs.num = 0
if self.rootfs.partition.endswith('1'): if pname.endswith('1'):
self.rootfs.num = 1 self.rootfs.num = 1
if verbose: if verbose:
print('RootFS info:') print('RootFS info:')
@ -379,6 +379,7 @@ class DevInfo():
def get_kernel_cmdline(self, verbose = None, retdict = True): def get_kernel_cmdline(self, verbose = None, retdict = True):
verbose = verbose if verbose is not None else self.verbose verbose = verbose if verbose is not None else self.verbose
self.kcmdline_s = ""
self.kcmdline = {} if retdict else None self.kcmdline = {} if retdict else None
fn_local = 'outdir/kcmdline.log' fn_local = 'outdir/kcmdline.log'
fn_remote = '/tmp/kcmdline.log' fn_remote = '/tmp/kcmdline.log'
@ -392,17 +393,18 @@ class DevInfo():
return self.kcmdline return self.kcmdline
if os.path.getsize(fn_local) <= 1: if os.path.getsize(fn_local) <= 1:
return self.kcmdline return self.kcmdline
with open(fn_local, "r", encoding="latin_1") as file: with open(fn_local, "rb") as file:
data = file.read() data = file.read()
data = data.replace(b"\n", b' ')
data = data.replace(b"\x00", b' ')
data = data.decode('latin_1')
data = data.strip()
self.kcmdline_s = data
if verbose: if verbose:
print("Kernel command line:") print("Kernel command line:")
print(" ", data) print(" ", data)
if not retdict: if not retdict:
return data return data
data = data.strip()
data = data.replace("\n", ' ')
data = data.replace("\x00", ' ')
data = data.strip()
env = EnvBuffer(data, ' ', crc_prefix = False, encoding = 'latin_1') env = EnvBuffer(data, ' ', crc_prefix = False, encoding = 'latin_1')
self.kcmdline = env.var self.kcmdline = env.var
#self.kcmdline = type("Names", [object], self.kcmdline) #self.kcmdline = type("Names", [object], self.kcmdline)

Loading…
Cancel
Save