-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdumpdecoder.py
More file actions
executable file
·126 lines (103 loc) · 3.34 KB
/
dumpdecoder.py
File metadata and controls
executable file
·126 lines (103 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import LDPC_decoder as LDPCdecoder
import scipy.io as scio
import numpy as np
import multiprocessing
import sys
import re
## LDPC Decoder for NAND Flash dump files
if(len(sys.argv)<3):
print("Usage: dumpdecoder.py <dumpfile.dump> <ldpcparameter.h> <geometry.case> <output.dump>")
exit(0)
inputdump=sys.argv[1]
ldpcparam=sys.argv[2]
geometry=sys.argv[3]
outputdump=sys.argv[4]
#pagesize = 4000
#dataeccpos = [0, 1500]
#datasize=1024
#eccsize=476
pagesize = 20
datapos = [0]
eccpos = [512]
dataeccpos = [0]
datasize=8
eccsize=8
with open(geometry,"r") as geo:
print("Reading values from geometry file "+geometry)
dataeccpos=[]
datapos=[]
eccpos=[]
for line in geo.readlines():
if re.search('<Page_size>(\d+)<\/Page_size>',line):
pagesize=int(re.search('<Page_size>(\d+)<\/Page_size>',line).group(1))
se=re.search('<Record StructureDefinitionName="(DA|Data area)" StartAddress="(\d+)" StopAddress="(\d+)" \/>',line)
if se:
dataeccpos.append(int(se.group(2))) # obsolete
datapos.append(int(se.group(2)))
datasize=int(se.group(3))-int(se.group(2))+1
se=re.search('<Record StructureDefinitionName="(ECC)" StartAddress="(\d+)" StopAddress="(\d+)" \/>',line)
if se:
eccpos.append(int(se.group(2)))
eccsize=int(se.group(3))-int(se.group(2))+1
if len(datapos)<1 or len(eccpos)<1:
print("Error: The geometry case file "+geometry+" does not contain enough data/ecc structures!")
exit(-1)
print("datapos: "+str(datapos))
print("eccpos: "+str(eccpos))
print("dataeccpos: "+str(dataeccpos))
print("pagesize: "+str(pagesize))
print("eccsize: "+str(eccsize))
## Noise
EbN = 3
print("EbN: "+str(EbN))
SNR_lin = 10**(EbN/10)
print("SNR_lin: "+str(SNR_lin))
No = 1.0/SNR_lin
print("No: "+str(No))
sigma = np.sqrt(No/2)
print("sigma: "+str(sigma))
n=int(re.search('_n(\d+)[_.]',ldpcparam).group(1))
print("n: "+str(n))
k=int(re.search('_k(\d+)[_.]',ldpcparam).group(1))
print("k: "+str(k))
m=int(re.search('_m(\d+)[_.]',ldpcparam).group(1))
print("m: "+str(m))
H=np.unpackbits(np.fromfile(ldpcparam,dtype=np.uint8),axis=None,bitorder='little').astype(np.int64).reshape(m,n)
#print("H shape: "+str(H.shape))
with open(inputdump,"rb") as dump:
with open(outputdump,"wb") as output:
while dump:
page = bytearray(dump.read(pagesize))
if len(page)<pagesize:
break
for pos in dataeccpos:
x = np.unpackbits(np.frombuffer(page[pos:pos+datasize+eccsize],dtype=np.uint8),axis=None,bitorder='little').astype(np.float32)
#print("x: "+str(x))
r = 2*x-1
#print("r: "+str(r))
#print("size of r: "+str(r.shape))
#print("H: "+str(H))
decoder = LDPCdecoder.decoder(H)
decoder.setInputMSA(r, sigma)
# Get Hard-Bits
w0 = r
w0[w0 >= 0] = 1
w0[w0 < 0] = 0
w0 = np.array(w0, dtype = int)
#ErrorUncoded = np.sum(w0 != x)
#print("Amount of Bit Errors (uncoded) : %d " % ErrorUncoded)
#MSA algorithm
for n in range(0,20):
decoded, y = decoder.iterateMinimumSumAlgorithm()
ErrorSPA = np.sum(y != x)
#print("Amount of Bit Errors (SPA) : %d " % ErrorSPA)
if(decoded):
break
ErrorSPA = np.sum(y != x)
print("Iterations: %d | Amount of Bit Errors (SPA) : %d " % (n, ErrorSPA))
#print("he")
#print(y)
#print("Original:")
#print(u)
page[pos:pos+datasize+eccsize]=np.packbits(decoded,axis=None,bitorder='little').tobytes()
output.write(page)