aboutsummaryrefslogtreecommitdiff
path: root/lib/xsh.exp
blob: 50ef49ca669c7bfd0c02023ebb92b849c6738612 (plain)
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# Copyright (C) 1992 - 2002, 2003 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# Please email any bugs, comments, and/or additions to this file to:
# bug-dejagnu@gnu.org

# This file was written by Rob Savoye. (rob@welcomehome.org)

#
# Connect to Spectra (VTRX) using xsh
#
proc xsh_open { hostname } {
    global hex
    global target_triplet
    global xsh_shell_prompt
    global board_info

    if [board_info $hostname exists fileid] {
	unset board_info($hostname,fileid)
    }

    if ![board_info $hostname exists spectra] {
	perror "No spectra directory for $hostname"
	return -1
    } else {
	set spectra [board_info $hostname spectra]
    }

    if ![board_info $hostname exists xsh_shell_prompt] {
	set xsh_shell_prompt ".*> "
    } else {
	set xsh_shell_prompt [board_info $hostname shell_prompt]
    }

    set retries 0
    set result  0
    if ![board_info $hostname exists xsh_prog] {
	set xsh xsh
    } else {
	set xsh [board_info $hostname xsh_prog]
    }

    if {[which $xsh] != 0} {
	spawn $xsh
    } else {
	warning "Can't find xsh in path"
	return -1
    }

    set shell_id $spawn_id

    # start the shell
    expect {
	"*Spectra Cross-Development Shell version*$xsh_shell_prompt" {
	    verbose "Got prompt"
	    set result 0
	}
	timeout			{
	    warning "Timed out trying to connect."
	    set result -1
	    incr retries
	    if { $retries <= 2 } {
		exp_continue
	    }
	}
    }

    # connect to the shell
    set retries 0
    send "connect $hostname\n"
    expect {
	"connect $hostname*$hostname connected \(non-os mode\)*\n" {
	    set xsh_shell_prompt "$hostname> "
	    verbose "Connected to $hostname"
	}
	"*connect: not attached*" {
	    warning "Couldn't attach target"
	    set result -1
	}
	-re ".* reset on target.*$" {
	    send_user "Spectra was reset\n"
	    exp_continue
	}
	-re "\[0-9A-Fa-f\]+\[ 0x\]+\[0-9A-Fa-f\]+.*$" {
	    exp_continue
	}
	"$hostname> " {
	    #send "\n"
	}
	timeout {
	    warning "Timed out trying to connect after $expect_out(seconds) seconds."
	    set result -1
	    incr retries
	    if { $retries <= 2 } {
		exp_continue
	    }
	}
    }

    send "\n\n\n"
    expect {
	"*$hostname*$hostname" {
	    verbose "Cleared reset messages" 1
	}
	timeout {
	    warning "Couldn't clear reset messages"
	    set result 1
	}
    }

    set board_info($hostname,fileid) $spawn_id
    # load to operating system
    set timeout 20
    set retries 0
    if {[xsh_download $hostname $spectra/${target_triplet}-os.o "" {-e sys_start_crt0}]!=0} {
	perror "Couldn't load Spectra into target"
	return -1
    }

    set timeout 10
    # start the OS running
    set retries 0
    send "go\n"
    expect {
	-re ".*Multithreading on target darkstar.*$" {
	    verbose "Spectra has been started..." 1
	    set result 0
	}
	-re ".*reset on target.*$" {
	    verbose "Spectra was reset"
	    exp_continue
	}
	-re "\[0-9A-Fa-f\]+\[ 0x\]+\[0-9A-Fa-f\]+.*$" {
	    #send "\n"
	    exp_continue
	}
	-re "go\n" { exp_continue }
	"$xsh_shell_prompt" { exp_continue }
	timeout {
	    perror "Spectra wouldn't start"
	    set result -1
	    incr retries
	    if { $retries <= 2 } {
		send "go\r"
		exp_continue
	    }
	}
    }

    if { $result < 0 } {
	perror "Couldn't connect after $retries retries.\n"
	return -1
    } else {
	set board_info($hostname,fileid) $spawn_id
	return $spawn_id
    }
}

#
# Download an executable using the load command in Spectra.
#     arg[0] - is a full path name to the file to download.
#     arg[1] - optional arguments to the load command.
#     returns  1 if a spectra error occured,
#             -1 if an internal error occured,
#              0 otherwise.
#
proc xsh_download { dest file destfile args } {
    global verbose
    global shell_id
    global decimal
    global hex
    global expect_out
    global board_info

    set result 1
    set retries 0
    set shell_id [board_info $dest fileid]

    if { [llength $args] > 1 } {
	set opts [lindex $args 1]
    } else {
	set opts ""
    }

    if { [llength $args] > 0 } {
	set destfile [lindex $args 0]
    }

    if ![file exists $file] {
	perror "$file doesn't exist."
	return 1
    }

    verbose "Downloading $file..."

    send -i $shell_id "load $opts $file\r"
    set force 0
    expect {
	-i $shell_id -re "\[0-9A-Fa-f\]+\[ 0x\]+\[0-9A-Fa-f\]+\r\n" {
	    set timeout 1
	    send "dout\n"
	    while $force<2 {
		expect {
		    "dout*undefined kernel symbol*$xsh_shell_prompt" {
			verbose "Attempted to flush I/O buffers" 1
		    }
		    timout {	
			incr force	
			flush stdout	
		    }
		}
	    }
	    set timeout 20
	    exp_continue
	}
	-i $shell_id  "load $opts $file*\r" {
	    verbose "Loading a.out..."
	    exp_continue
	}
	-i $shell_id "Warm reset on target*\n" {
	    verbose "Spectra did a warm reset"
	    exp_continue
	}
	-i $shell_id "Cold reset on target*\n" {
	    verbose "Spectra did a cold reset"
	    exp_continue
	}
	-i $shell_id "loading a.out*\r" {
	    verbose "Loading a.out..."
	    exp_continue
	}
	-i $shell_id "reading symbols*\r" {
	    verbose "Reading symbols..."
	    exp_continue
	}
	-i $shell_id "defining symbols*\r" {
	    verbose "defining symbols..."
	    exp_continue
	}
	-i $shell_id "*loading image*\r" {
	    verbose "Loading image..."
	    exp_continue
	}
	-i $shell_id -re ".*bytes loaded:.*$decimal.*$" {
	    verbose "$expect_out(buffer)"
	    exp_continue
	}
	-i $shell_id "*loading done*\r" {
	    verbose "Loading done..."
	    exp_continue
	}
	-i $shell_id "*setting PC*\r" {
	    verbose "Setting PC..."
	    exp_continue
	}
	-i $shell_id "*resolving symbols*\r" {
	    verbose "Resolving symbols..."
	    exp_continue
	}
        -i $shell_id -re ".*load module id = $decimal.*$" {
	    verbose ""
        }
	-i $shell_id -re ".*load: undefined symbols.*$"  {
	    perror "undefined symbols, make sure os is loaded and running"
	    set result -1
        }
	-i $shell_id "$xsh_shell_prompt" {
	    set result 0
	    exp_continue
	}
	-i $shell_id "load: no default target" {
	    perror "default target isn't set"
	    return -1
	}
	-i $shell_id timeout {
	    perror "Timed out trying to download after $expect_out(seconds) seconds."
	    incr retries
	    set result 1
	    if { $retries <= 2 } {
		exp_continue
	    }
	}
    }

    set timeout 10
    if [info exists expect_out(buffer)] {
	send_log $expect_out(buffer)
    }
    set board_info($hostname,fileid) $shell_id
    return $result
}

#
# Exit the remote shell
#
proc xsh_close { hostname } {
    global board_info

    if ![board_info $hostname exists fileid] {
	return
    }

    set shell_id [board_info ${hostname} fileid]
    send -i $shell_id "exit\n"
    unset board_info(${hostname},fileid)

    verbose "Exiting shell."
    return 0
}