This file is indexed.

/usr/bin/samba-program is in tinyos-tools 1.4.2-3.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
#!/usr/bin/python2

# Script to program the Atmel SAM3U using the sam-ba tool.

import os
import re
import sys
import time
import signal
import optparse
import tempfile
import subprocess

parser = optparse.OptionParser()

parser.add_option("-p", "--port",
        action="store",
        type="string",
        dest="port",
        default="/dev/ttyUSB0",
        help="Port where the SAM3U can be found.")
parser.add_option("-b", "--binfile",
        action="store",
        type="string",
        dest="binfile",
        default="build/sam3u_ek/main.bin.out",
        help="Binary file that should be programmed into the flash.")
parser.add_option("-t", "--target",
        action="store",
        type="string",
        dest="target",
        default="AT91SAM3U4-EK",
        help="Target board type.")
parser.add_option("-c", "--check",
        action="store_true",
        dest="check",
        default=False,
        help="Checks and verifies the programmed flash")
parser.add_option("-r", "--run",
        action="store_true",
        dest="run",
        default=False,
        help="Starts executing the binary after flashing it")
parser.add_option("-s", "--start-addr",
        action="store",
        type="string",
        dest="start_addr",
        default="0x80000",
        help="Determines the start address where the binary is loaded")
parser.add_option("-d", "--debug",
        action="store_true",
        dest="DEBUG",
        default=False,
        help="Set the debug mode.")

(cmdOptions, args) = parser.parse_args()
class samba:
    def __init__(self):
        self.expect_timeout = False

        # check to make sure binary file exists
        if not os.path.isfile(cmdOptions.binfile):
            print '"%s" does not exist. Exiting.' % cmdOptions.binfile
            sys.exit(1)
        # once we switch to python 2.6, we should do this
        #self.f = tempfile.NamedTemporaryFile(delete=False)

        #if sam3s, use different TCL script
        r = re.compile("sam3s")
        matches = r.findall(cmdOptions.target)
        if(len(matches) != 0):
            print "Found SAM3S"

            self.f = file('/tmp/samba.tcl', 'w+')
            self.f.write("""FLASH::Init
        send_file {Flash} "%s" %s 0
        FLASH::ScriptGPNMV 2
        """%(cmdOptions.binfile,cmdOptions.start_addr))
            if cmdOptions.check:
                # verify flash
                print "Verify image"
                self.f.write('compare_file {Flash} "%s" %s 0\n'%(cmdOptions.binfile,cmdOptions.start_addr))
            if cmdOptions.run:
                # automatically run the code after writing
                self.f.write("TCL_Go $target(handle) 0 0\n")
        else:

            if( int(cmdOptions.start_addr, 16) >= 0x80000 and int(cmdOptions.start_addr, 16) < 0x100000):
                flash_id = 0
            else:
                flash_id = 1
            self.f = file('/tmp/samba.tcl', 'w+')
            self.f.write("""FLASH::Init %d
        send_file {Flash %d} "%s" %s 0
        FLASH::ScriptGPNMV 2
        """%(flash_id,flash_id,cmdOptions.binfile,cmdOptions.start_addr))
            if cmdOptions.check:
                # verify flash
                print "Verify image"
                self.f.write('compare_file {Flash %d} "%s" %s 0\n'%(flash_id, cmdOptions.binfile,cmdOptions.start_addr))
            if cmdOptions.run:
                # automatically run the code after writing
                self.f.write("TCL_Go $target(handle) 0 0\n")
        self.f.flush()

        try:
            error = False

            # check if SAMBA bootloader is here
            foundBootloader = False
            while not foundBootloader:
                print "Checking for programmer"
                lsusb_proc = subprocess.Popen('lsusb -d 03eb:6124', shell=True,
                        stdout=subprocess.PIPE)
                r = re.compile("SAMBA bootloader")
                lsusb_proc.wait()
                matches = r.findall(lsusb_proc.stdout.readline())
                if len(matches) == 0:
                    print """\n Couldn't find SAM-BA bootloader device on the USB bus.
     Please close the ERASE jumper on the development kit and reboot the system (hit NRSTB button)!\n"""
                    time.sleep(2)
                else:
                    foundBootloader = True

            print "Programmer Found!"

            print "Programming..."
            #print "Remove ERASE and hit [Enter]"
            #a = raw_input()

            samba_cmd = "DISPLAY=:0 sam-ba %s %s %s"%(cmdOptions.port, cmdOptions.target,
                    self.f.name)
            if(cmdOptions.DEBUG):
                print "DEBUG: ", samba_cmd
            samba_proc = subprocess.Popen(samba_cmd, shell=True, stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE)
            r = re.compile("sam-ba: not found")
            e = samba_proc.stderr.readline()

            if len(r.findall(samba_proc.stderr.readline())) != 0:
                print "Couldn't find 'sam-ba'. Please make sure it is in your PATH!"
                self.cleanup()
                sys.exit(1)
            try:
                self.expect(samba_proc.stdout, "-I- Found processor : at91sam3")
            except RuntimeError:
                print "Couldn't find processor! Make sure the port '%s' is correct."%(cmdOptions.port)
                self.cleanup()
                sys.exit(1)
            try:
                self.expect(samba_proc.stdout, "-I- Command line mode : Execute script file")
            except:
                print "Couldn't execute script!"
                self.cleanup()
                sys.exit(1)
            try:
                self.expect(samba_proc.stdout, "-I- GPNVM1 set")
            except:
                print "Couldn't program the device!"
                self.cleanup()
                sys.exit(1)

            if cmdOptions.check:
                try:
                    self.expect(samba_proc.stdout, "match exactly")
                except:
                    print "Verification failed!"
                    self.cleanup()
                    sys.exit(1)

            if cmdOptions.run:
                print "Done! Your code should be running now."
            else:
                print "Done! Reboot your system (hit NRSTB button)."

        finally:
            pass

    def cleanup(self):
        self.f.close()
        os.unlink(self.f.name)


    def alarmHandler(self, signum, frame):
        self.expect_timeout = True

    # Wait until expected pattern is received on the given filehandle.
    def expect(self, fh, pat, timeout=3):
        r = re.compile(pat)

        expect_found = False

        if (timeout != -1):
            signal.signal(signal.SIGALRM, self.alarmHandler)
            signal.alarm(timeout)

        while (not expect_found and not self.expect_timeout):
            line = fh.readline().strip()
            if cmdOptions.DEBUG:
                print line
                time.sleep(0.1)
            matches = r.findall(line)
            if (len(matches) != 0):
                expect_found = True
                break

        signal.alarm(0)
        if (not expect_found):
            raise RuntimeError, "Did not receive expected pattern '%s'" % pat


if __name__ == "__main__":
    s = samba()