forked from GrassLab/osdi2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraspbootcom.py
More file actions
executable file
·164 lines (130 loc) · 4.56 KB
/
Copy pathraspbootcom.py
File metadata and controls
executable file
·164 lines (130 loc) · 4.56 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
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
#!/usr/bin/python3
import serial
import os
import sys
import logging, coloredlogs
import termios
import string
class _Getch:
"""Gets a single character from standard input. Does not echo to the
screen."""
def __init__(self):
try:
self.impl = _GetchWindows()
except ImportError:
self.impl = _GetchUnix()
def __call__(self): return self.impl()
class _GetchUnix:
def __init__(self):
import tty, sys
def __call__(self):
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
class _GetchWindows:
def __init__(self):
import msvcrt
def __call__(self):
import msvcrt
return msvcrt.getch()
'''
create logger
'''
coloredlogs.install()
logger = logging.getLogger(__file__)
logger.setLevel(logging.DEBUG)
'''
get kernel image size
'''
# KERNEL_IMG_PATH = "/media/sf_Documents/OSDI/KERNEL8.IMG"
# KERNEL_IMG_PATH = "/media/sf_Documents/OSDI/osdi2020/kernel8.img"
# KERNEL_IMG_PATH = "/media/sf_Documents/OSDI/raspi3-tutorial/0C_directory/kernel8.img"
KERNEL_IMG_PATH = "/media/sf_Documents/OSDI/osdi2020/kernel_test/sd/kernel/kernel8.img"
KERNEL_IMG_SIZE = os.stat(KERNEL_IMG_PATH).st_size
def send_size(s, kernel_size):
print(s.readline()) ### b"Please send the kernel size...\r\n"
s.write(bytes( [kernel_size & 0xff] ))
s.write(bytes( [(kernel_size >> 8) & 0xff] ))
s.write(bytes( [(kernel_size >> 16) & 0xff] ))
s.write(bytes( [(kernel_size >> 24) & 0xff] ))
raspi3_recv = b""
for _ in range(2):
raspi3_recv += s.read()
print(raspi3_recv)
if (raspi3_recv != b"OK"):
return False
return True
def send_kernel(s, kernel_size):
f = open(KERNEL_IMG_PATH, "rb")
while (kernel_size>0):
s.write(f.read())
kernel_size -= 1
def main():
s = serial.Serial("/dev/ttyUSB0", 115200)
logger.info(f'kernel size: {KERNEL_IMG_SIZE}')
logger.info("waiting...")
while True:
NO_REBOOT = True
while NO_REBOOT:
res_line = b''
while True:
res = s.read()
res_line += res
#print(f'read: {res}')
if (res_line == b'>'):
break
if (res == b'\n'):
print(res_line)
sys.stdout.flush()
if b'HANK0438\r\n' in res_line:
logger.info("rebooting...")
NO_REBOOT = False
break
res_line = b''
if NO_REBOOT:
#termios.tcflush(sys.stdin, termios.TCIOFLUSH)
command = input(">").strip()
logger.info(f"cmd > {command}")
s.write(command.encode()+b'\n')
logger.info("sending size...")
if send_size(s, KERNEL_IMG_SIZE):
print(s.readline()) ### b"Please input the kernel load address (default: 0x80000):\r\n"
print(s.readline()) ### b"Please send the kernel from UART...\n"
logger.info("sending kernel...")
send_kernel(s, KERNEL_IMG_SIZE)
logger.info("finish!")
print(s.readline()) ### b'Loading kernel at 0x00100000 with size 0x00007010 ...\r\n'
# logger.info("starting interactive mode...")
# getch = _Getch()
# while True:
# serial_read_line = b''
# stdin_read_line = b''
# while True:
# stdin_read_char = getch().encode()#sys.stdin.read(1)
# if stdin_read_char is not b'\r':
# try:
# if stdin_read_char not in [__ascii.encode() for __ascii in string.printable]:
# print("non-printable!!")
# exit(0)
# except:
# print("error")
# exit(0)
# s.write(b"\n")
# else:
# s.write(stdin_read_char)
# stdin_read_line += stdin_read_char
# serial_read_char = s.read()
# serial_read_line += serial_read_char
# if (serial_read_char == b'\n'):
# sys.stdout.write("\n")
# sys.stdout.flush()
# else:
# sys.stdout.write("\r" + serial_read_line.decode())
# sys.stdout.flush()
main()