-
Notifications
You must be signed in to change notification settings - Fork 0
/
netfpga.py
executable file
·304 lines (270 loc) · 9.65 KB
/
netfpga.py
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env python
#netfpga.py - A simple web.py application allow remote debugging on Altera FPGA
#Project NetFPGA created by Shengye Wang, Fudan University 01/25/2013
#Inspired by Prof. Michael Taylor, UC San Diego
#See http://shengye.me for more information
#This file may be used or modified with no restriction.
#Raw copies as well as modified copies may be distributed without limitation.
import web
import time
import random
import socket
import os
render = web.template.render('templates')
#Change the following lines to meet actual
MAX_PER_TIME = 60.0 #max protection time
filedir = '/home/shengye/netfpga/upload_files' # change this to the directory you want to store the file in.
HOST = 'localhost'
PORT = 1337 #modify with the port number in tcl script
CMD_LEN = 47 #length of string send to jtag server
NUM_LEN = 34 #length data field
session_time = time.time()
session_id = '000000000'
status = "IDLE" #global variable holding current status.
def get_status(): #update and return status
global status
global session_time
past_time = time.time() - session_time
if (past_time < 0 or past_time > MAX_PER_TIME) and (status == "PROTECTED"):
status = "UNPROTECTED"
return status
def set_status(new_status): #set status
global status
global session_id
status = new_status
if (status == "IDLE"):
session_id = '000000000'
def check_sid(sid): #check if client is the current user
global session_id
return sid == session_id
def conn(): #connect to TCP server
global sock
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
except socket.error, msg:
return ("[ERROR] %s\n" % msg[1])
def send_recv(buf): #send data to jtag server and get the response
global sock
#Send
bcnt = sock.send(buf+'\n')
#print "Send is " + buf + " len = " + str(bcnt)
#Receive
buf = sock.recv(1024)
#print "Recv is " + buf + " len = " + str(len(buf))
return buf
def num_to_str(num, leng = NUM_LEN): #convert integer to binary stored in string(reversed)
rtn = ''
for i in range(leng):
rtn = str(1 if (num & (1 << i)) > 0 else 0) + rtn
rtn = rtn[::-1] #Revert return value
return rtn
def str_to_num(s, signext = False): #convert binary stored in string(reversed) to integer
s = s[::-1] #Revert s
rtn = 0
if (s[0] == '1' and signext):
for i in range(len(s)):
rtn += 2**(len(s) - 1 - i) if (s[i] == '0') else 0
rtn += 1
rtn = -rtn
else:
for i in range(len(s)):
rtn += 2**(len(s) - 1 - i) if (s[i] == '1') else 0
return rtn
urls = (
'/', 'index',
'/new', 'New',
'/giveup', 'GiveUp',
'/upload', 'Upload',
'/program', 'Program',
'/startserver', 'StartServer',
'/inittarget', 'InitTarget',
'/interaction', 'Interaction',
'/nav', 'Nav'
)
class index:
def GET(self):
global session_time
global MAX_PER_TIME
web.header("Content-Type","text/html; charset=utf-8")
page = "Status is " + get_status()
return render.index(get_status(), check_sid(web.cookies().get('sid')), "%.1f" % (time.time() - session_time), "%.1f" % (MAX_PER_TIME))
class New:
def GET(self):
global session_time
global session_id
web.header("Content-Type","text/html; charset=utf-8")
content = ""
showNavBar = False #Should show the navigation bar
if (get_status() == "PROTECTED"):
if check_sid(web.cookies().get('sid')):
content += "Serving current user. Please give up before creating new session.\n"
showNavBar = True
else:
content += "Serving other user in protected period.\nACCESS DENIED.\n"
else:
content += "Killing background utilities.\n"
content += os.popen("./stopall.sh").read()
content += "Genearting new session ID.\n"
session_time = time.time()
new_sid = str(random.randint(100000000, 999999999)); #sid is 9 digits integer in string
session_id = new_sid
set_status("PROTECTED")
content += "New seesion ID is " + str(new_sid) + ".\n"
content += "Status is " + get_status() + ".\n"
web.setcookie("sid", new_sid, expires=3600)
showNavBar = True
return render.new(get_status(), showNavBar, content)
class GiveUp:
def GET(self):
web.header("Content-Type","text/html; charset=utf-8")
content = ""
showNavBar = False #Should show the navigation bar
if check_sid(web.cookies().get('sid')):
content += "Giving up.\n"
content += "Killing background utilities.\n"
content += os.popen("./stopall.sh").read()
set_status("IDLE")
content += "Status is " + get_status() + ".\n"
showNavBar = True
else:
content += "Serving other user.\nACCESS DENIED.\n"
return render.giveup(get_status(), showNavBar, content)
class Upload:
def GET(self):
web.header("Content-Type","text/html; charset=utf-8")
content = ""
if check_sid(web.cookies().get('sid')):
pass
else:
content += "Serving other user.\nACCESS DENIED.\n"
return render.upload(get_status(), check_sid(web.cookies().get('sid')), content, check_sid(web.cookies().get('sid')))
def POST(self):
web.header("Content-Type","text/html; charset=utf-8")
content = ""
if check_sid(web.cookies().get('sid')):
x = web.input(myfile={})
global filedir
if 'myfile' in x: # to check if the file-object is created
filepath=x.myfile.filename.replace('\\','/') # replaces the windows-style slashes with linux ones.
filename=filepath.split('/')[-1] # splits the and chooses the last part (the filename with extension)
fout = open(filedir +'/current.sof','w') # creates the file where the uploaded file should be stored
fout.write(x.myfile.file.read()) # writes the uploaded file to the newly created file.
fout.close() # closes the file, upload complete.
os.popen('cp ' + filedir +'/current.sof' + ' ' + filedir +'/' + web.cookies().get('sid') + '.sof').read() #make a copy to store sof file, comment this if not needed
content += "File Accepted.\n"
else:
content += "File not received.\n"
else:
content += "Serving other user.\nACCESS DENIED.\n"
return render.upload(get_status(), check_sid(web.cookies().get('sid')), content, check_sid(web.cookies().get('sid')))
class Program:
def GET(self):
web.header("Content-Type","text/html; charset=utf-8")
content = ""
if check_sid(web.cookies().get('sid')):
#content += "Killing background utilities.\n"
content += os.popen("./stopall.sh").read()
#content += "Programming.\n"
content += os.popen("./program.sh").read()
else:
content += "Serving other user.\nACCESS DENIED.\n"
return render.program(get_status(), check_sid(web.cookies().get('sid')), content)
class StartServer:
def GET(self):
web.header("Content-Type","text/html; charset=utf-8")
content = ""
if check_sid(web.cookies().get('sid')):
content += "Killing background utilities.\n"
content += os.popen("./stopall.sh").read()
content += "Starting server.\n"
content += os.popen("./tcpserver.sh").read()
content += "Please continue.\n"
else:
content += "Serving other user.\nACCESS DENIED.\n"
return render.startserver(get_status(), check_sid(web.cookies().get('sid')), content)
class InitTarget:
def GET(self):
web.header("Content-Type","text/html; charset=utf-8")
content = ""
if check_sid(web.cookies().get('sid')):
connrtn = conn()
if (connrtn == None):
send_recv(46 * '0' + '1') # Reset
send_recv(47 * '0') # Release reset signal
content += "Core reseted.\n"
else:
content += "Communication Failed.\n" + connrtn
else:
content += "Serving other user.\nACCESS DENIED.\n"
return render.inittarget(get_status(), check_sid(web.cookies().get('sid')), content)
class Interaction:
def GET(self):
web.header("Content-Type","text/html; charset=utf-8")
content = ""
inputReq = False
autoRefresh = False
inputChannel = ""
tryMore = True
if check_sid(web.cookies().get('sid')):
connrtn = conn()
if (connrtn == None):
while tryMore:
buf = send_recv(47 * '0')
if (buf[42] == '1'): #output request
res = "Output request #" + str(str_to_num(buf[34:38])) + " : " + str(str_to_num(buf[0:34], signext = True));
buf = 44 * '0' + '100'; #set out_ack
send_recv(buf)
buf = 44 * '0' + '000'; #set out_ack
send_recv(buf)
io_history = open('interaction.txt', 'a')
io_history.write(res + "\n")
io_history.close()
else:
inputReq = buf[43] == '1'
inputChannel = str(str_to_num(buf[38:42]))
tryMore = False
autoRefresh = not inputReq
sock.close()
else:
content += "Communication Failed.\n" + connrtn
io_history = open('interaction.txt', 'r')
content += io_history.read()
io_history.close()
else:
content += "Serving other user.\nACCESS DENIED.\n"
return render.interaction(get_status(), check_sid(web.cookies().get('sid')), content, autoRefresh, inputReq, inputChannel)
def POST(self):
web.header("Content-Type","text/html; charset=utf-8")
content = ""
if check_sid(web.cookies().get('sid')):
connrtn = conn()
if (connrtn == None):
try:
num = int(web.input().get('val', None))
except:
content += "Invalid input, assuming 0\n"
num = 0
buf = send_recv(47 * '0')
if (buf[43] == '1'): #input request
res = "Input request #" + str(str_to_num(buf[38:42])) + " : " + str(num)
buf = num_to_str(num) + 10 * '0' + '010'; #set in_ack
send_recv(buf)
buf = num_to_str(num) + 10 * '0' + '000'; #clr in_ack
send_recv(buf)
io_history = open('interaction.txt', 'a')
io_history.write(res + "\n")
io_history.close()
sock.close()
raise web.seeother('/interaction')
else:
content += connrtn
else:
content += "Serving other user.\nACCESS DENIED.\n"
return render.interaction(get_status(), check_sid(web.cookies().get('sid')), content, False, False, '')
class Nav:
def GET(self):
return render.nav(get_status())
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()