summaryrefslogtreecommitdiffstats
path: root/Src/osmoconbb/src/host/osmocon
diff options
context:
space:
mode:
Diffstat (limited to 'Src/osmoconbb/src/host/osmocon')
-rw-r--r--Src/osmoconbb/src/host/osmocon/.gitignore36
-rw-r--r--Src/osmoconbb/src/host/osmocon/COPYING339
-rw-r--r--Src/osmoconbb/src/host/osmocon/Makefile.am21
-rw-r--r--Src/osmoconbb/src/host/osmocon/configure.ac25
-rwxr-xr-xSrc/osmoconbb/src/host/osmocon/git-version-gen151
-rwxr-xr-xSrc/osmoconbb/src/host/osmocon/memdump_convert.pl29
-rw-r--r--Src/osmoconbb/src/host/osmocon/osmocon.c1558
-rw-r--r--Src/osmoconbb/src/host/osmocon/osmoload.c1216
-rw-r--r--Src/osmoconbb/src/host/osmocon/tpu_debug.c138
9 files changed, 3513 insertions, 0 deletions
diff --git a/Src/osmoconbb/src/host/osmocon/.gitignore b/Src/osmoconbb/src/host/osmocon/.gitignore
new file mode 100644
index 0000000..ad061b7
--- /dev/null
+++ b/Src/osmoconbb/src/host/osmocon/.gitignore
@@ -0,0 +1,36 @@
+# autoreconf by-products
+*.in
+
+aclocal.m4
+autom4te.cache/
+configure
+depcomp
+install-sh
+missing
+
+# configure by-products
+.deps/
+Makefile
+
+config.status
+version.h
+
+# build by-products
+*.o
+
+osmocon
+osmoload
+
+# various
+.version
+.tarball-version
+
+# IDA file
+*.id*
+*.nam
+*.til
+
+# Other test files
+*.dump
+*.bin
+*.log
diff --git a/Src/osmoconbb/src/host/osmocon/COPYING b/Src/osmoconbb/src/host/osmocon/COPYING
new file mode 100644
index 0000000..d511905
--- /dev/null
+++ b/Src/osmoconbb/src/host/osmocon/COPYING
@@ -0,0 +1,339 @@
+ GNU GENERAL PUBLIC LICENSE
+ Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+ Preamble
+
+ The licenses for most software are designed to take away your
+freedom to share and change it. By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users. This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it. (Some other Free Software Foundation software is covered by
+the GNU Lesser General Public License instead.) You can apply it to
+your programs, too.
+
+ When we speak of free software, we are referring to freedom, not
+price. Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+ To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+ For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have. You must make sure that they, too, receive or can get the
+source code. And you must show them these terms so they know their
+rights.
+
+ We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+ Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software. If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+ Finally, any free program is threatened constantly by software
+patents. We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary. To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+ The precise terms and conditions for copying, distribution and
+modification follow.
+
+ GNU GENERAL PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License. The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language. (Hereinafter, translation is included without limitation in
+the term "modification".) Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope. The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+ 1. You may copy and distribute verbatim copies of the Program's
+source code as you receive it, in any medium, provided that you
+conspicuously and appropriately publish on each copy an appropriate
+copyright notice and disclaimer of warranty; keep intact all the
+notices that refer to this License and to the absence of any warranty;
+and give any other recipients of the Program a copy of this License
+along with the Program.
+
+You may charge a fee for the physical act of transferring a copy, and
+you may at your option offer warranty protection in exchange for a fee.
+
+ 2. You may modify your copy or copies of the Program or any portion
+of it, thus forming a work based on the Program, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+ a) You must cause the modified files to carry prominent notices
+ stating that you changed the files and the date of any change.
+
+ b) You must cause any work that you distribute or publish, that in
+ whole or in part contains or is derived from the Program or any
+ part thereof, to be licensed as a whole at no charge to all third
+ parties under the terms of this License.
+
+ c) If the modified program normally reads commands interactively
+ when run, you must cause it, when started running for such
+ interactive use in the most ordinary way, to print or display an
+ announcement including an appropriate copyright notice and a
+ notice that there is no warranty (or else, saying that you provide
+ a warranty) and that users may redistribute the program under
+ these conditions, and telling the user how to view a copy of this
+ License. (Exception: if the Program itself is interactive but
+ does not normally print such an announcement, your work based on
+ the Program is not required to print an announcement.)
+
+These requirements apply to the modified work as a whole. If
+identifiable sections of that work are not derived from the Program,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works. But when you
+distribute the same sections as part of a whole which is a work based
+on the Program, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Program.
+
+In addition, mere aggregation of another work not based on the Program
+with the Program (or with a work based on the Program) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+ 3. You may copy and distribute the Program (or a work based on it,
+under Section 2) in object code or executable form under the terms of
+Sections 1 and 2 above provided that you also do one of the following:
+
+ a) Accompany it with the complete corresponding machine-readable
+ source code, which must be distributed under the terms of Sections
+ 1 and 2 above on a medium customarily used for software interchange; or,
+
+ b) Accompany it with a written offer, valid for at least three
+ years, to give any third party, for a charge no more than your
+ cost of physically performing source distribution, a complete
+ machine-readable copy of the corresponding source code, to be
+ distributed under the terms of Sections 1 and 2 above on a medium
+ customarily used for software interchange; or,
+
+ c) Accompany it with the information you received as to the offer
+ to distribute corresponding source code. (This alternative is
+ allowed only for noncommercial distribution and only if you
+ received the program in object code or executable form with such
+ an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for
+making modifications to it. For an executable work, complete source
+code means all the source code for all modules it contains, plus any
+associated interface definition files, plus the scripts used to
+control compilation and installation of the executable. However, as a
+special exception, the source code distributed need not include
+anything that is normally distributed (in either source or binary
+form) with the major components (compiler, kernel, and so on) of the
+operating system on which the executable runs, unless that component
+itself accompanies the executable.
+
+If distribution of executable or object code is made by offering
+access to copy from a designated place, then offering equivalent
+access to copy the source code from the same place counts as
+distribution of the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+ 4. You may not copy, modify, sublicense, or distribute the Program
+except as expressly provided under this License. Any attempt
+otherwise to copy, modify, sublicense or distribute the Program is
+void, and will automatically terminate your rights under this License.
+However, parties who have received copies, or rights, from you under
+this License will not have their licenses terminated so long as such
+parties remain in full compliance.
+
+ 5. You are not required to accept this License, since you have not
+signed it. However, nothing else grants you permission to modify or
+distribute the Program or its derivative works. These actions are
+prohibited by law if you do not accept this License. Therefore, by
+modifying or distributing the Program (or any work based on the
+Program), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+ 6. Each time you redistribute the Program (or any work based on the
+Program), the recipient automatically receives a license from the
+original licensor to copy, distribute or modify the Program subject to
+these terms and conditions. You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
+this License.
+
+ 7. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Program at all. For example, if a patent
+license would not permit royalty-free redistribution of the Program by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Program.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system, which is
+implemented by public license practices. Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+ 8. If the distribution and/or use of the Program is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Program under this License
+may add an explicit geographical distribution limitation excluding
+those countries, so that distribution is permitted only in or among
+countries not thus excluded. In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+ 9. The Free Software Foundation may publish revised and/or new versions
+of the General Public License from time to time. Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+Each version is given a distinguishing version number. If the Program
+specifies a version number of this License which applies to it and "any
+later version", you have the option of following the terms and conditions
+either of that version or of any later version published by the Free
+Software Foundation. If the Program does not specify a version number of
+this License, you may choose any version ever published by the Free Software
+Foundation.
+
+ 10. If you wish to incorporate parts of the Program into other free
+programs whose distribution conditions are different, write to the author
+to ask for permission. For software which is copyrighted by the Free
+Software Foundation, write to the Free Software Foundation; we sometimes
+make exceptions for this. Our decision will be guided by the two goals
+of preserving the free status of all derivatives of our free software and
+of promoting the sharing and reuse of software generally.
+
+ NO WARRANTY
+
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
+TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
+PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
+REPAIR OR CORRECTION.
+
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
+REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
+INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
+OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
+TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
+YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
+PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGES.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Programs
+
+ If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+ To do so, attach the following notices to the program. It is safest
+to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+ <one line to give the program's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ 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.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+Also add information on how to contact you by electronic and paper mail.
+
+If the program is interactive, make it output a short notice like this
+when it starts in an interactive mode:
+
+ Gnomovision version 69, Copyright (C) year name of author
+ Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+ This is free software, and you are welcome to redistribute it
+ under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License. Of course, the commands you use may
+be called something other than `show w' and `show c'; they could even be
+mouse-clicks or menu items--whatever suits your program.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the program, if
+necessary. Here is a sample; alter the names:
+
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the program
+ `Gnomovision' (which makes passes at compilers) written by James Hacker.
+
+ <signature of Ty Coon>, 1 April 1989
+ Ty Coon, President of Vice
+
+This General Public License does not permit incorporating your program into
+proprietary programs. If your program is a subroutine library, you may
+consider it more useful to permit linking proprietary applications with the
+library. If this is what you want to do, use the GNU Lesser General
+Public License instead of this License.
diff --git a/Src/osmoconbb/src/host/osmocon/Makefile.am b/Src/osmoconbb/src/host/osmocon/Makefile.am
new file mode 100644
index 0000000..8b0d4bf
--- /dev/null
+++ b/Src/osmoconbb/src/host/osmocon/Makefile.am
@@ -0,0 +1,21 @@
+AUTOMAKE_OPTIONS = foreign dist-bzip2 1.6
+
+# versioning magic
+BUILT_SOURCES = $(top_srcdir)/.version
+$(top_srcdir)/.version:
+ echo $(VERSION) > $@-t && mv $@-t $@
+dist-hook:
+ echo $(VERSION) > $(distdir)/.tarball-version
+
+INCLUDES = $(all_includes) -I$(top_srcdir)/include
+AM_CFLAGS=-Wall $(LIBOSMOCORE_CFLAGS)
+
+sbin_PROGRAMS = osmocon osmoload
+
+# FIXME: sercomm needs to move into libosmocore or another shared lib
+INCLUDES += -I../../target/firmware/include/comm -I../../target/firmware/apps -DHOST_BUILD
+osmocon_SOURCES = osmocon.c tpu_debug.c ../../target/firmware/comm/sercomm.c
+osmocon_LDADD = $(LIBOSMOCORE_LIBS)
+
+osmoload_SOURCE = osmoload.c ../../target/firmware/comm/sercomm.c
+osmoload_LDADD = $(LIBOSMOCORE_LIBS)
diff --git a/Src/osmoconbb/src/host/osmocon/configure.ac b/Src/osmoconbb/src/host/osmocon/configure.ac
new file mode 100644
index 0000000..4130800
--- /dev/null
+++ b/Src/osmoconbb/src/host/osmocon/configure.ac
@@ -0,0 +1,25 @@
+dnl Process this file with autoconf to produce a configure script
+AC_INIT([osmocon],
+ m4_esyscmd([./git-version-gen .tarball-version]),
+ [baseband-devel@lists.osmocom.org])
+
+AM_INIT_AUTOMAKE([dist-bzip2])
+
+dnl kernel style compile messages
+m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
+
+dnl checks for programs
+AC_PROG_MAKE_SET
+AC_PROG_CC
+AC_PROG_INSTALL
+
+dnl checks for libraries
+PKG_CHECK_MODULES(LIBOSMOCORE, libosmocore)
+
+dnl checks for header files
+AC_HEADER_STDC
+
+dnl Checks for typedefs, structures and compiler characteristics
+
+AC_OUTPUT(
+ Makefile)
diff --git a/Src/osmoconbb/src/host/osmocon/git-version-gen b/Src/osmoconbb/src/host/osmocon/git-version-gen
new file mode 100755
index 0000000..652fac6
--- /dev/null
+++ b/Src/osmoconbb/src/host/osmocon/git-version-gen
@@ -0,0 +1,151 @@
+#!/bin/sh
+# Print a version string.
+scriptversion=2010-01-28.01
+
+# Copyright (C) 2007-2010 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 3 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, see <http://www.gnu.org/licenses/>.
+
+# This script is derived from GIT-VERSION-GEN from GIT: http://git.or.cz/.
+# It may be run two ways:
+# - from a git repository in which the "git describe" command below
+# produces useful output (thus requiring at least one signed tag)
+# - from a non-git-repo directory containing a .tarball-version file, which
+# presumes this script is invoked like "./git-version-gen .tarball-version".
+
+# In order to use intra-version strings in your project, you will need two
+# separate generated version string files:
+#
+# .tarball-version - present only in a distribution tarball, and not in
+# a checked-out repository. Created with contents that were learned at
+# the last time autoconf was run, and used by git-version-gen. Must not
+# be present in either $(srcdir) or $(builddir) for git-version-gen to
+# give accurate answers during normal development with a checked out tree,
+# but must be present in a tarball when there is no version control system.
+# Therefore, it cannot be used in any dependencies. GNUmakefile has
+# hooks to force a reconfigure at distribution time to get the value
+# correct, without penalizing normal development with extra reconfigures.
+#
+# .version - present in a checked-out repository and in a distribution
+# tarball. Usable in dependencies, particularly for files that don't
+# want to depend on config.h but do want to track version changes.
+# Delete this file prior to any autoconf run where you want to rebuild
+# files to pick up a version string change; and leave it stale to
+# minimize rebuild time after unrelated changes to configure sources.
+#
+# It is probably wise to add these two files to .gitignore, so that you
+# don't accidentally commit either generated file.
+#
+# Use the following line in your configure.ac, so that $(VERSION) will
+# automatically be up-to-date each time configure is run (and note that
+# since configure.ac no longer includes a version string, Makefile rules
+# should not depend on configure.ac for version updates).
+#
+# AC_INIT([GNU project],
+# m4_esyscmd([build-aux/git-version-gen .tarball-version]),
+# [bug-project@example])
+#
+# Then use the following lines in your Makefile.am, so that .version
+# will be present for dependencies, and so that .tarball-version will
+# exist in distribution tarballs.
+#
+# BUILT_SOURCES = $(top_srcdir)/.version
+# $(top_srcdir)/.version:
+# echo $(VERSION) > $@-t && mv $@-t $@
+# dist-hook:
+# echo $(VERSION) > $(distdir)/.tarball-version
+
+case $# in
+ 1) ;;
+ *) echo 1>&2 "Usage: $0 \$srcdir/.tarball-version"; exit 1;;
+esac
+
+tarball_version_file=$1
+nl='
+'
+
+# First see if there is a tarball-only version file.
+# then try "git describe", then default.
+if test -f $tarball_version_file
+then
+ v=`cat $tarball_version_file` || exit 1
+ case $v in
+ *$nl*) v= ;; # reject multi-line output
+ [0-9]*) ;;
+ *) v= ;;
+ esac
+ test -z "$v" \
+ && echo "$0: WARNING: $tarball_version_file seems to be damaged" 1>&2
+fi
+
+if test -n "$v"
+then
+ : # use $v
+elif
+ v=`git describe --abbrev=4 --match='osmocon_v*' HEAD 2>/dev/null \
+ || git describe --abbrev=4 HEAD 2>/dev/null` \
+ && case $v in
+ osmocon_[0-9]*) ;;
+ osmocon_v[0-9]*) ;;
+ *) (exit 1) ;;
+ esac
+then
+ # Is this a new git that lists number of commits since the last
+ # tag or the previous older version that did not?
+ # Newer: v6.10-77-g0f8faeb
+ # Older: v6.10-g0f8faeb
+ case $v in
+ *-*-*) : git describe is okay three part flavor ;;
+ *-*)
+ : git describe is older two part flavor
+ # Recreate the number of commits and rewrite such that the
+ # result is the same as if we were using the newer version
+ # of git describe.
+ vtag=`echo "$v" | sed 's/-.*//'`
+ numcommits=`git rev-list "$vtag"..HEAD | wc -l`
+ v=`echo "$v" | sed "s/\(.*\)-\(.*\)/\1-$numcommits-\2/"`;
+ ;;
+ esac
+
+ # Change the first '-' to a '.', so version-comparing tools work properly.
+ # Remove the "g" in git describe's output string, to save a byte.
+ v=`echo "$v" | sed 's/-/./;s/\(.*\)-g/\1-/;s/^osmocon_//'`;
+else
+ v="UNKNOWN"
+fi
+
+v=`echo "$v" |sed 's/^v//'`
+
+# Don't declare a version "dirty" merely because a time stamp has changed.
+git status > /dev/null 2>&1
+
+dirty=`sh -c 'git diff-index --name-only HEAD' 2>/dev/null` || dirty=
+case "$dirty" in
+ '') ;;
+ *) # Append the suffix only if there isn't one already.
+ case $v in
+ *-dirty) ;;
+ *) v="$v-dirty" ;;
+ esac ;;
+esac
+
+# Omit the trailing newline, so that m4_esyscmd can use the result directly.
+echo "$v" | tr -d '\012'
+
+# Local variables:
+# eval: (add-hook 'write-file-hooks 'time-stamp)
+# time-stamp-start: "scriptversion="
+# time-stamp-format: "%:y-%02m-%02d.%02H"
+# time-stamp-end: "$"
+# End:
diff --git a/Src/osmoconbb/src/host/osmocon/memdump_convert.pl b/Src/osmoconbb/src/host/osmocon/memdump_convert.pl
new file mode 100755
index 0000000..3d18a0b
--- /dev/null
+++ b/Src/osmoconbb/src/host/osmocon/memdump_convert.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/perl
+
+my $num_line = 0;
+my $num_hex = 0;
+my $oldaddr;
+
+while (my $line = <STDIN>) {
+ chomp($line);
+ $num_line++;
+ my (@hex) = $line =~ /(\w{8}): (\w{8}) (\w{8}) (\w{8}) (\w{8}) (\w{8}) (\w{8}) (\w{8}) (\w{8})/;
+ my $addr = hex(shift @hex);
+ if ($addr != 0 && $addr != $oldaddr + 0x20) {
+ printf(STDERR "gap of %u between 0x%08x and 0x%08x\n%s\n",
+ $addr - $oldaddr, $addr, $oldaddr, $line);
+ }
+ foreach my $h (@hex) {
+ $num_hex++;
+ # poor mans endian conversion
+ my ($a, $b, $c, $d) = $h =~/(\w\w)(\w\w)(\w\w)(\w\w)/;
+ my $h_reorder = $d . $c . $b . $a;
+ # convert into actual binary number
+ my $tmp = pack('H8', $h_reorder);
+ syswrite(STDOUT, $tmp, 4);
+ }
+ $oldaddr = $addr;
+}
+
+printf(STDERR "num lines/num hex: %u/%u\n", $num_line, $num_hex);
+
diff --git a/Src/osmoconbb/src/host/osmocon/osmocon.c b/Src/osmoconbb/src/host/osmocon/osmocon.c
new file mode 100644
index 0000000..7f74f8f
--- /dev/null
+++ b/Src/osmoconbb/src/host/osmocon/osmocon.c
@@ -0,0 +1,1558 @@
+/* osmocon */
+
+/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
+ * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
+ * (C) 2010 by Steve Markgraf <steve@steve-m.de>
+ *
+ * All Rights Reserved
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <termios.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/un.h>
+
+#include <sercomm.h>
+
+#include <osmocom/core/linuxlist.h>
+#include <osmocom/core/select.h>
+#include <osmocom/core/talloc.h>
+#include <osmocom/core/timer.h>
+
+#include <arpa/inet.h>
+
+#define MODEM_BAUDRATE B115200
+#define MAX_DNLOAD_SIZE 0xFFFF
+#define MAX_HDR_SIZE 128
+#define MAGIC_OFFSET 0x3be2
+
+#define DEFAULT_BEACON_INTERVAL 50000
+#define ROMLOAD_INIT_BAUDRATE B19200
+#define ROMLOAD_DL_BAUDRATE B115200
+#define ROMLOAD_BLOCK_HDR_LEN 10
+#define ROMLOAD_ADDRESS 0x820000
+
+#define MTK_INIT_BAUDRATE B19200
+#define MTK_ADDRESS 0x40001400
+#define MTK_BLOCK_SIZE 1024
+
+struct tool_server *tool_server_for_dlci[256];
+
+/**
+ * a connection from some other tool
+ */
+struct tool_connection {
+ struct tool_server *server;
+ struct llist_head entry;
+ struct osmo_fd fd;
+};
+
+/**
+ * server for a tool
+ */
+struct tool_server {
+ struct osmo_fd bfd;
+ uint8_t dlci;
+ struct llist_head connections;
+};
+
+
+enum dnload_state {
+ WAITING_PROMPT1,
+ WAITING_PROMPT2,
+ DOWNLOADING,
+};
+
+enum romload_state {
+ WAITING_IDENTIFICATION,
+ WAITING_PARAM_ACK,
+ SENDING_BLOCKS,
+ SENDING_LAST_BLOCK,
+ LAST_BLOCK_SENT,
+ WAITING_BLOCK_ACK,
+ WAITING_CHECKSUM_ACK,
+ WAITING_BRANCH_ACK,
+ FINISHED,
+};
+
+enum mtk_state {
+ MTK_INIT_1,
+ MTK_INIT_2,
+ MTK_INIT_3,
+ MTK_INIT_4,
+ MTK_WAIT_WRITE_ACK,
+ MTK_WAIT_ADDR_ACK,
+ MTK_WAIT_SIZE_ACK,
+ MTK_SENDING_BLOCKS,
+ MTK_WAIT_BRANCH_CMD_ACK,
+ MTK_WAIT_BRANCH_ADDR_ACK,
+ MTK_FINISHED,
+};
+
+enum dnload_mode {
+ MODE_C123,
+ MODE_C123xor,
+ MODE_C140,
+ MODE_C140xor,
+ MODE_C155,
+ MODE_ROMLOAD,
+ MODE_MTK,
+};
+
+struct dnload {
+ enum dnload_state state;
+ enum romload_state romload_state;
+ enum mtk_state mtk_state;
+ enum dnload_mode mode;
+ struct osmo_fd serial_fd;
+ char *filename;
+ char *chainload_filename;
+
+ int expect_hdlc;
+
+ int dump_rx;
+ int dump_tx;
+ int beacon_interval;
+
+ /* data to be downloaded */
+ uint8_t *data;
+ int data_len;
+
+ uint8_t *write_ptr;
+
+ /* romload: block to be downloaded */
+ uint8_t *block;
+ int block_len;
+ uint8_t block_number;
+ uint16_t block_payload_size;
+ int romload_dl_checksum;
+ uint8_t *block_ptr;
+ uint8_t load_address[4];
+
+ uint8_t mtk_send_size[4];
+ int block_count;
+ int echo_bytecount;
+
+ struct tool_server layer2_server;
+ struct tool_server loader_server;
+};
+
+
+static struct dnload dnload;
+static struct osmo_timer_list tick_timer;
+
+/* Compal ramloader specific */
+static const uint8_t phone_prompt1[] = { 0x1b, 0xf6, 0x02, 0x00, 0x41, 0x01, 0x40 };
+static const uint8_t dnload_cmd[] = { 0x1b, 0xf6, 0x02, 0x00, 0x52, 0x01, 0x53 };
+static const uint8_t phone_prompt2[] = { 0x1b, 0xf6, 0x02, 0x00, 0x41, 0x02, 0x43 };
+static const uint8_t phone_ack[] = { 0x1b, 0xf6, 0x02, 0x00, 0x41, 0x03, 0x42 };
+static const uint8_t phone_nack_magic[]= { 0x1b, 0xf6, 0x02, 0x00, 0x41, 0x03, 0x57 };
+static const uint8_t phone_nack[] = { 0x1b, 0xf6, 0x02, 0x00, 0x45, 0x53, 0x16 };
+static const uint8_t ftmtool[] = { 0x66, 0x74, 0x6d, 0x74, 0x6f, 0x6f, 0x6c };
+static const uint8_t phone_magic[] = { 0x31, 0x30, 0x30, 0x33 }; /* "1003" */
+
+/* The C123 has a hard-coded check inside the ramloader that requires the
+ * following bytes to be always the first four bytes of the image */
+static const uint8_t data_hdr_c123[] = { 0xee, 0x4c, 0x9f, 0x63 };
+
+/* The C155 doesn't have some strange restriction on what the first four bytes
+ * have to be, but it starts the ramloader in THUMB mode. We use the following
+ * four bytes to switch back to ARM mode:
+ 800100: 4778 bx pc
+ 800102: 46c0 nop ; (mov r8, r8)
+ */
+static const uint8_t data_hdr_c155[] = { 0x78, 0x47, 0xc0, 0x46 };
+
+/* Calypso romloader specific */
+static const uint8_t romload_ident_cmd[] = { 0x3c, 0x69 }; /* <i */
+static const uint8_t romload_abort_cmd[] = { 0x3c, 0x61 }; /* <a */
+static const uint8_t romload_write_cmd[] = { 0x3c, 0x77 }; /* <w */
+static const uint8_t romload_checksum_cmd[] = { 0x3c, 0x63 }; /* <c */
+static const uint8_t romload_branch_cmd[] = { 0x3c, 0x62 }; /* <b */
+static const uint8_t romload_ident_ack[] = { 0x3e, 0x69 }; /* >i */
+static const uint8_t romload_param_ack[] = { 0x3e, 0x70 }; /* >p */
+static const uint8_t romload_param_nack[] = { 0x3e, 0x50 }; /* >P */
+static const uint8_t romload_block_ack[] = { 0x3e, 0x77 }; /* >w */
+static const uint8_t romload_block_nack[] = { 0x3e, 0x57 }; /* >W */
+static const uint8_t romload_checksum_ack[] = { 0x3e, 0x63 }; /* >c */
+static const uint8_t romload_checksum_nack[] = { 0x3e, 0x43 }; /* >C */
+static const uint8_t romload_branch_ack[] = { 0x3e, 0x62 }; /* >b */
+static const uint8_t romload_branch_nack[] = { 0x3e, 0x42 }; /* >B */
+
+/* romload_param: {"<p", uint8_t baudrate, uint8_t dpll, uint16_t memory_config,
+ * uint8_t strobe_af, uint32_t uart_timeout} */
+
+static const uint8_t romload_param[] = { 0x3c, 0x70, 0x00, 0x00, 0x00, 0x04,
+ 0x00, 0x00, 0x00, 0x00, 0x00 };
+
+/* MTK romloader specific */
+static const uint8_t mtk_init_cmd[] = { 0xa0, 0x0a, 0x50, 0x05 };
+static const uint8_t mtk_init_resp[] = { 0x5f, 0xf5, 0xaf, 0xfa };
+static const uint8_t mtk_command[] = { 0xa1, 0xa2, 0xa4, 0xa8 };
+
+/* FIXME: this routine is more or less what openbsc/src/rs232:rs232_setup()
+ * does, we should move it to libosmocore at some point */
+static int serial_init(const char *serial_port)
+{
+ int rc, serial_fd, v24;
+ struct termios tio;
+
+ serial_fd = open(serial_port, O_RDWR | O_NOCTTY | O_NDELAY);
+ if (serial_fd < 0) {
+ perror("cannot open serial port");
+ return serial_fd;
+ }
+
+ //fcntl(serial_fd, F_SETFL, 0);
+
+ /* Configure serial interface */
+ rc = tcgetattr(serial_fd, &tio);
+ if (rc < 0) {
+ perror("tcgetattr()");
+ return rc;
+ }
+ cfsetispeed(&tio, MODEM_BAUDRATE);
+ cfsetospeed(&tio, MODEM_BAUDRATE);
+ tio.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
+ tio.c_cflag |= (CREAD | CLOCAL | CS8);
+ tio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
+ tio.c_iflag |= (INPCK | ISTRIP);
+ tio.c_iflag &= ~(ISTRIP | IXON | IXOFF | IGNBRK | INLCR | ICRNL | IGNCR);
+ tio.c_oflag &= ~(OPOST | ONLCR);
+ rc = tcsetattr(serial_fd, TCSANOW, &tio);
+ if (rc < 0) {
+ perror("tcsetattr()");
+ return rc;
+ }
+
+ /* set ready to read/write */
+ v24 = TIOCM_DTR | TIOCM_RTS;
+ rc = ioctl(serial_fd, TIOCMBIS, &v24);
+ if (rc < 0) {
+ perror("ioctl(TIOCMBIS)");
+ return rc;
+ }
+
+ return serial_fd;
+}
+
+static int serial_set_baudrate(speed_t baudrate)
+{
+ int rc;
+ struct termios tio;
+
+ rc = tcgetattr(dnload.serial_fd.fd, &tio);
+ if (rc < 0) {
+ perror("tcgetattr()");
+ return rc;
+ }
+ cfsetispeed(&tio, baudrate);
+ cfsetospeed(&tio, baudrate);
+
+ rc = tcsetattr(dnload.serial_fd.fd, TCSANOW, &tio);
+ return rc;
+}
+
+static void beacon_timer_cb(void *p)
+{
+ int rc;
+
+ if (dnload.romload_state == WAITING_IDENTIFICATION) {
+ printf("Sending Calypso romloader beacon...\n");
+ rc = write(dnload.serial_fd.fd, romload_ident_cmd,
+ sizeof(romload_ident_cmd));
+
+ if (!(rc == sizeof(romload_ident_cmd)))
+ printf("Error sending identification beacon\n");
+
+ osmo_timer_schedule(p, 0, dnload.beacon_interval);
+ }
+}
+
+static void mtk_timer_cb(void *p)
+{
+ int rc;
+
+ if (dnload.mtk_state == MTK_INIT_1) {
+ printf("Sending MTK romloader beacon...\n");
+ rc = write(dnload.serial_fd.fd, &mtk_init_cmd[0], 1);
+
+ if (!(rc == 1))
+ printf("Error sending identification beacon\n");
+
+ osmo_timer_schedule(p, 0, dnload.beacon_interval);
+ }
+}
+
+/* Read the to-be-downloaded file, prepend header and length, append XOR sum */
+int read_file(const char *filename)
+{
+ int fd, rc, i;
+ struct stat st;
+ const uint8_t *hdr = NULL;
+ int payload_size;
+ int hdr_len = 0;
+ uint8_t *file_data;
+ uint16_t tot_len;
+ uint8_t nibble;
+ uint8_t running_xor = 0x02;
+
+ fd = open(filename, O_RDONLY);
+ if (fd < 0) {
+ perror("opening file");
+ exit(1);
+ }
+
+ rc = fstat(fd, &st);
+ if (st.st_size > MAX_DNLOAD_SIZE) {
+ fprintf(stderr, "The maximum file size is 64kBytes (%u bytes)\n",
+ MAX_DNLOAD_SIZE);
+ return -EFBIG;
+ }
+
+ free(dnload.data);
+ dnload.data = NULL;
+
+ if (dnload.mode == MODE_C140 || dnload.mode == MODE_C140xor) {
+ if (st.st_size < (MAGIC_OFFSET + sizeof(phone_magic)))
+ payload_size = MAGIC_OFFSET + sizeof(phone_magic);
+ else {
+ printf("\nThe filesize is larger than 15kb, code on "
+ "the magic address will be overwritten!\nUse "
+ "loader.bin and upload the application with "
+ "osmoload instead!\n\n");
+ payload_size = st.st_size;
+ }
+ } else
+ payload_size = st.st_size;
+
+ dnload.data = malloc(MAX_HDR_SIZE + payload_size);
+
+ if (!dnload.data) {
+ close(fd);
+ fprintf(stderr, "No memory\n");
+ return -ENOMEM;
+ }
+
+ /* copy in the header, if any */
+ switch (dnload.mode) {
+ case MODE_C155:
+ hdr = data_hdr_c155;
+ hdr_len = sizeof(data_hdr_c155);
+ break;
+ case MODE_C140:
+ case MODE_C140xor:
+ case MODE_C123:
+ case MODE_C123xor:
+ hdr = data_hdr_c123;
+ hdr_len = sizeof(data_hdr_c123);
+ break;
+ case MODE_ROMLOAD:
+ break;
+ default:
+ break;
+ }
+
+ if (hdr && hdr_len)
+ memcpy(dnload.data, hdr, hdr_len);
+
+ /* 2 bytes for length + header */
+ file_data = dnload.data + 2 + hdr_len;
+
+ /* write the length, keep running XOR */
+ tot_len = hdr_len + payload_size;
+ nibble = tot_len >> 8;
+ dnload.data[0] = nibble;
+ running_xor ^= nibble;
+ nibble = tot_len & 0xff;
+ dnload.data[1] = nibble;
+ running_xor ^= nibble;
+
+ if (hdr_len && hdr) {
+ memcpy(dnload.data+2, hdr, hdr_len);
+
+ for (i = 0; i < hdr_len; i++)
+ running_xor ^= hdr[i];
+ }
+
+ rc = read(fd, file_data, st.st_size);
+ if (rc < 0) {
+ perror("error reading file\n");
+ free(dnload.data);
+ dnload.data = NULL;
+ close(fd);
+ return -EIO;
+ }
+ if (rc < st.st_size) {
+ free(dnload.data);
+ dnload.data = NULL;
+ close(fd);
+ fprintf(stderr, "Short read of file (%d < %d)\n",
+ rc, (int)st.st_size);
+ return -EIO;
+ }
+
+ close(fd);
+
+ dnload.data_len = (file_data+payload_size) - dnload.data;
+
+ /* fill memory between data end and magic, add magic */
+ if(dnload.mode == MODE_C140 || dnload.mode == MODE_C140xor) {
+ if (st.st_size < MAGIC_OFFSET)
+ memset(file_data + st.st_size, 0x00,
+ payload_size - st.st_size);
+ memcpy(dnload.data + MAGIC_OFFSET, phone_magic,
+ sizeof(phone_magic));
+ }
+
+ /* calculate XOR sum */
+ for (i = 0; i < payload_size; i++)
+ running_xor ^= file_data[i];
+
+ dnload.data[dnload.data_len++] = running_xor;
+
+ /* initialize write pointer to start of data */
+ dnload.write_ptr = dnload.data;
+
+ printf("read_file(%s): file_size=%u, hdr_len=%u, dnload_len=%u\n",
+ filename, (int)st.st_size, hdr_len, dnload.data_len);
+
+ return 0;
+}
+
+static void osmocon_osmo_hexdump(const uint8_t *data, unsigned int len)
+{
+ int n;
+
+ for (n=0; n < len; n++)
+ printf("%02x ", data[n]);
+ printf(" ");
+ for (n=0; n < len; n++)
+ if (isprint(data[n]))
+ putchar(data[n]);
+ else
+ putchar('.');
+ printf("\n");
+}
+
+static int romload_prepare_block(void)
+{
+ int i;
+
+ int block_checksum = 5;
+ int remaining_bytes;
+ int fill_bytes;
+ uint8_t *block_data;
+ uint32_t block_address;
+
+ dnload.block_len = ROMLOAD_BLOCK_HDR_LEN + dnload.block_payload_size;
+
+ /* if first block, allocate memory */
+ if (!dnload.block_number) {
+ dnload.block = malloc(dnload.block_len);
+ if (!dnload.block) {
+ fprintf(stderr, "No memory\n");
+ return -ENOMEM;
+ }
+ dnload.romload_dl_checksum = 0;
+ /* initialize write pointer to start of data */
+ dnload.write_ptr = dnload.data;
+ }
+
+ block_address = ROMLOAD_ADDRESS +
+ (dnload.block_number * dnload.block_payload_size);
+
+ /* prepare our block header (10 bytes) */
+ memcpy(dnload.block, romload_write_cmd, sizeof(romload_write_cmd));
+ dnload.block[2] = 0x01; /* block index */
+ /* should normally be the block number, but hangs when sending !0x01 */
+ dnload.block[3] = 0x01; /* dnload.block_number+1 */
+ dnload.block[4] = (dnload.block_payload_size >> 8) & 0xff;
+ dnload.block[5] = dnload.block_payload_size & 0xff;
+ dnload.block[6] = (block_address >> 24) & 0xff;
+ dnload.block[7] = (block_address >> 16) & 0xff;
+ dnload.block[8] = (block_address >> 8) & 0xff;
+ dnload.block[9] = block_address & 0xff;
+
+ block_data = dnload.block + ROMLOAD_BLOCK_HDR_LEN;
+ dnload.write_ptr = dnload.data + 2 +
+ (dnload.block_payload_size * dnload.block_number);
+
+ remaining_bytes = dnload.data_len - 3 -
+ (dnload.block_payload_size * dnload.block_number);
+
+ memcpy(block_data, dnload.write_ptr, dnload.block_payload_size);
+
+ if (remaining_bytes <= dnload.block_payload_size) {
+ fill_bytes = (dnload.block_payload_size - remaining_bytes);
+ printf("Preparing the last block, filling %i bytes,",
+ fill_bytes);
+ memset(block_data + remaining_bytes, 0x00, fill_bytes);
+ dnload.romload_state = SENDING_LAST_BLOCK;
+ } else {
+ dnload.romload_state = SENDING_BLOCKS;
+ printf("Preparing block %i,", dnload.block_number+1);
+ }
+
+ /* block checksum is lsb of ~(5 + block_size_lsb + all bytes of
+ * block_address + all data bytes) */
+ for (i = 5; i < ROMLOAD_BLOCK_HDR_LEN + dnload.block_payload_size; i++)
+ block_checksum += dnload.block[i];
+
+ /* checksum is lsb of ~(sum of LSBs of all block checksums) */
+ printf(" block checksum is 0x%02x \n", ~(block_checksum) & 0xff);
+ dnload.romload_dl_checksum += ~(block_checksum) & 0xff;
+
+ /* initialize block pointer to start of block */
+ dnload.block_ptr = dnload.block;
+
+ dnload.block_number++;
+ dnload.serial_fd.when = BSC_FD_READ | BSC_FD_WRITE;
+ return 0;
+}
+
+static int mtk_prepare_block(void)
+{
+ int rc, i;
+
+ int remaining_bytes;
+ int fill_bytes;
+ uint8_t *block_data;
+ uint8_t tmp_byteswap;
+ uint32_t tmp_size;
+
+ dnload.block_len = MTK_BLOCK_SIZE;
+ dnload.echo_bytecount = 0;
+
+ /* if first block, allocate memory */
+ if (!dnload.block_number) {
+ dnload.block = malloc(dnload.block_len);
+ if (!dnload.block) {
+ fprintf(stderr, "No memory\n");
+ return -ENOMEM;
+ }
+
+ /* calculate the number of blocks we need to send */
+ dnload.block_count = (dnload.data_len-3) / MTK_BLOCK_SIZE;
+ /* add one more block if no multiple of blocksize */
+ if((dnload.data_len-3) % MTK_BLOCK_SIZE)
+ dnload.block_count++;
+
+ /* divide by 2, since we have to tell the mtk loader the size
+ * as count of uint16 (odd transfer sizes are not possible) */
+ tmp_size = (dnload.block_count * MTK_BLOCK_SIZE)/2;
+ dnload.mtk_send_size[0] = (tmp_size >> 24) & 0xff;
+ dnload.mtk_send_size[1] = (tmp_size >> 16) & 0xff;
+ dnload.mtk_send_size[2] = (tmp_size >> 8) & 0xff;
+ dnload.mtk_send_size[3] = tmp_size & 0xff;
+
+ /* initialize write pointer to start of data */
+ dnload.write_ptr = dnload.data;
+ }
+
+ block_data = dnload.block;
+ dnload.write_ptr = dnload.data + 2 +
+ (dnload.block_len * dnload.block_number);
+
+ remaining_bytes = dnload.data_len - 3 -
+ (dnload.block_len * dnload.block_number);
+
+ memcpy(block_data, dnload.write_ptr, MTK_BLOCK_SIZE);
+
+ if (remaining_bytes <= MTK_BLOCK_SIZE) {
+ fill_bytes = (MTK_BLOCK_SIZE - remaining_bytes);
+ printf("Preparing the last block, filling %i bytes\n",
+ fill_bytes);
+ memset(block_data + remaining_bytes, 0x00, fill_bytes);
+ dnload.romload_state = SENDING_LAST_BLOCK;
+ } else {
+ dnload.romload_state = SENDING_BLOCKS;
+ printf("Preparing block %i\n", dnload.block_number+1);
+ }
+
+ /* for the mtk romloader we need to swap MSB <-> LSB */
+ for (i = 0; i < dnload.block_len; i += 2) {
+ tmp_byteswap = dnload.block[i];
+ dnload.block[i] = dnload.block[i+1];
+ dnload.block[i+1] = tmp_byteswap;
+ }
+
+ /* initialize block pointer to start of block */
+ dnload.block_ptr = dnload.block;
+
+ dnload.block_number++;
+ return rc;
+}
+
+static int handle_write_block(void)
+{
+ int bytes_left, write_len, rc;
+
+ printf("handle_write_block(): ");
+
+ if (dnload.block_ptr >= dnload.block + dnload.block_len) {
+ printf("Block %i finished\n", dnload.block_number);
+ dnload.write_ptr = dnload.data;
+ dnload.serial_fd.when &= ~BSC_FD_WRITE;
+ if (dnload.romload_state == SENDING_LAST_BLOCK) {
+ dnload.romload_state = LAST_BLOCK_SENT;
+ printf("Finished, sent %i blocks in total\n",
+ dnload.block_number);
+ } else {
+ dnload.romload_state = WAITING_BLOCK_ACK;
+ }
+
+ return 0;
+ }
+
+ /* try to write a maximum of block_len bytes */
+ bytes_left = (dnload.block + dnload.block_len) - dnload.block_ptr;
+ write_len = dnload.block_len;
+ if (bytes_left < dnload.block_len)
+ write_len = bytes_left;
+
+ rc = write(dnload.serial_fd.fd, dnload.block_ptr, write_len);
+ if (rc < 0) {
+ perror("Error during write");
+ return rc;
+ }
+
+ dnload.block_ptr += rc;
+
+ printf("%u bytes (%tu/%u)\n", rc, dnload.block_ptr - dnload.block,
+ dnload.block_len);
+
+ return 0;
+}
+
+#define WRITE_BLOCK 4096
+
+static int handle_write_dnload(void)
+{
+ int bytes_left, write_len, rc;
+ uint8_t xor_init = 0x02;
+
+ printf("handle_write(): ");
+ if (dnload.write_ptr == dnload.data) {
+ /* no bytes have been transferred yet */
+ switch (dnload.mode) {
+ case MODE_C155:
+ case MODE_C140xor:
+ case MODE_C123xor:
+ rc = write(dnload.serial_fd.fd, &xor_init, 1);
+ break;
+ default:
+ break;
+ }
+ } else if (dnload.write_ptr >= dnload.data + dnload.data_len) {
+ printf("finished\n");
+ dnload.write_ptr = dnload.data;
+ dnload.serial_fd.when &= ~BSC_FD_WRITE;
+ return 1;
+ }
+
+ /* try to write a maximum of WRITE_BLOCK bytes */
+ bytes_left = (dnload.data + dnload.data_len) - dnload.write_ptr;
+ write_len = WRITE_BLOCK;
+ if (bytes_left < WRITE_BLOCK)
+ write_len = bytes_left;
+
+ rc = write(dnload.serial_fd.fd, dnload.write_ptr, write_len);
+ if (rc < 0) {
+ perror("Error during write");
+ return rc;
+ }
+
+ dnload.write_ptr += rc;
+
+ printf("%u bytes (%tu/%u)\n", rc, dnload.write_ptr - dnload.data,
+ dnload.data_len);
+
+ return 0;
+}
+
+static int handle_sercomm_write(void)
+{
+ uint8_t c;
+
+ if (sercomm_drv_pull(&c) != 0) {
+ if (write(dnload.serial_fd.fd, &c, 1) != 1)
+ perror("short write");
+ } else
+ dnload.serial_fd.when &= ~BSC_FD_WRITE;
+
+ return 0;
+}
+
+static int handle_write(void)
+{
+ /* TODO: simplify this again (global state: downloading, sercomm) */
+ switch (dnload.mode) {
+ case MODE_ROMLOAD:
+ switch (dnload.romload_state) {
+ case SENDING_BLOCKS:
+ case SENDING_LAST_BLOCK:
+ return handle_write_block();
+ default:
+ return handle_sercomm_write();
+ }
+ break;
+ case MODE_MTK:
+ switch (dnload.mtk_state) {
+ case MTK_SENDING_BLOCKS:
+ return handle_write_block();
+ default:
+ return handle_sercomm_write();
+ }
+ break;
+ default:
+ switch (dnload.state) {
+ case DOWNLOADING:
+ return handle_write_dnload();
+ default:
+ return handle_sercomm_write();
+ }
+ }
+
+ return 0;
+}
+
+static uint8_t buffer[sizeof(phone_prompt1)];
+static uint8_t *bufptr = buffer;
+
+static void hdlc_send_to_phone(uint8_t dlci, uint8_t *data, int len)
+{
+ struct msgb *msg;
+ uint8_t *dest;
+
+ if(dnload.dump_tx) {
+ printf("hdlc_send(dlci=%u): ", dlci);
+ osmocon_osmo_hexdump(data, len);
+ }
+
+ if (len > 512) {
+ fprintf(stderr, "Too much data to send. %u\n", len);
+ return;
+ }
+
+ /* push the message into the stack */
+ msg = sercomm_alloc_msgb(512);
+ if (!msg) {
+ fprintf(stderr, "Failed to create data for the frame.\n");
+ return;
+ }
+
+ /* copy the data */
+ dest = msgb_put(msg, len);
+ memcpy(dest, data, len);
+
+ sercomm_sendmsg(dlci, msg);
+
+ dnload.serial_fd.when |= BSC_FD_WRITE;
+}
+
+static void hdlc_console_cb(uint8_t dlci, struct msgb *msg)
+{
+ write(1, msg->data, msg->len);
+ msgb_free(msg);
+}
+
+static void hdlc_tool_cb(uint8_t dlci, struct msgb *msg)
+{
+ struct tool_server *srv = tool_server_for_dlci[dlci];
+
+ if(dnload.dump_rx) {
+ printf("hdlc_recv(dlci=%u): ", dlci);
+ osmocon_osmo_hexdump(msg->data, msg->len);
+ }
+
+ if(srv) {
+ struct tool_connection *con;
+ uint16_t *len;
+
+ len = (uint16_t *) msgb_push(msg, 2);
+ *len = htons(msg->len - sizeof(*len));
+
+ llist_for_each_entry(con, &srv->connections, entry) {
+ if (write(con->fd.fd, msg->data, msg->len) != msg->len) {
+ fprintf(stderr,
+ "Failed to write msg to the socket..\n");
+ continue;
+ }
+ }
+ }
+
+ msgb_free(msg);
+}
+
+static int handle_buffer(int buf_used_len)
+{
+ int nbytes, buf_left, i;
+
+ buf_left = buf_used_len - (bufptr - buffer);
+ if (buf_left <= 0) {
+ memmove(buffer, buffer+1, buf_used_len-1);
+ bufptr -= 1;
+ buf_left = 1;
+ }
+
+ nbytes = read(dnload.serial_fd.fd, bufptr, buf_left);
+ if (nbytes <= 0)
+ return nbytes;
+
+ if (!dnload.expect_hdlc) {
+ printf("got %i bytes from modem, ", nbytes);
+ printf("data looks like: ");
+ osmocon_osmo_hexdump(bufptr, nbytes);
+ } else {
+ for (i = 0; i < nbytes; ++i)
+ if (sercomm_drv_rx_char(bufptr[i]) == 0)
+ printf("Dropping sample '%c'\n", bufptr[i]);
+ }
+
+ return nbytes;
+}
+
+/* Compal ramloader */
+static int handle_read(void)
+{
+ int rc, nbytes;
+
+ nbytes = handle_buffer(sizeof(buffer));
+ if (nbytes <= 0)
+ return nbytes;
+
+ if (!memcmp(buffer, phone_prompt1, sizeof(phone_prompt1))) {
+ printf("Received PROMPT1 from phone, responding with CMD\n");
+ dnload.expect_hdlc = 0;
+ dnload.state = WAITING_PROMPT2;
+ if(dnload.filename) {
+ rc = write(dnload.serial_fd.fd, dnload_cmd, sizeof(dnload_cmd));
+
+ /* re-read file */
+ rc = read_file(dnload.filename);
+ if (rc < 0) {
+ fprintf(stderr, "read_file(%s) failed with %d\n",
+ dnload.filename, rc);
+ exit(1);
+ }
+ }
+ } else if (!memcmp(buffer, phone_prompt2, sizeof(phone_prompt2))) {
+ printf("Received PROMPT2 from phone, starting download\n");
+ dnload.serial_fd.when = BSC_FD_READ | BSC_FD_WRITE;
+ dnload.state = DOWNLOADING;
+ } else if (!memcmp(buffer, phone_ack, sizeof(phone_ack))) {
+ printf("Received DOWNLOAD ACK from phone, your code is"
+ " running now!\n");
+ dnload.serial_fd.when = BSC_FD_READ;
+ dnload.state = WAITING_PROMPT1;
+ dnload.write_ptr = dnload.data;
+ dnload.expect_hdlc = 1;
+
+ /* check for romloader chainloading mode used as a workaround
+ * for the magic on the C139/C140 and J100i */
+ if (dnload.chainload_filename != NULL) {
+ printf("Enabled Compal ramloader -> Calypso romloader"
+ " chainloading mode\n");
+ bufptr = buffer;
+ dnload.filename = dnload.chainload_filename;
+ dnload.mode = MODE_ROMLOAD;
+ serial_set_baudrate(ROMLOAD_INIT_BAUDRATE);
+ tick_timer.cb = &beacon_timer_cb;
+ tick_timer.data = &tick_timer;
+ osmo_timer_schedule(&tick_timer, 0, dnload.beacon_interval);
+ }
+ } else if (!memcmp(buffer, phone_nack, sizeof(phone_nack))) {
+ printf("Received DOWNLOAD NACK from phone, something went"
+ " wrong :(\n");
+ dnload.serial_fd.when = BSC_FD_READ;
+ dnload.state = WAITING_PROMPT1;
+ dnload.write_ptr = dnload.data;
+ } else if (!memcmp(buffer, phone_nack_magic, sizeof(phone_nack_magic))) {
+ printf("Received MAGIC NACK from phone, you need to"
+ " have \"1003\" at 0x803ce0\n");
+ dnload.serial_fd.when = BSC_FD_READ;
+ dnload.state = WAITING_PROMPT1;
+ dnload.write_ptr = dnload.data;
+ } else if (!memcmp(buffer, ftmtool, sizeof(ftmtool))) {
+ printf("Received FTMTOOL from phone, ramloader has aborted\n");
+ dnload.serial_fd.when = BSC_FD_READ;
+ dnload.state = WAITING_PROMPT1;
+ dnload.write_ptr = dnload.data;
+ }
+ bufptr += nbytes;
+
+ return nbytes;
+}
+
+/* "Calypso non-secure romloader" */
+static int handle_read_romload(void)
+{
+ int rc, nbytes, buf_used_len;
+
+ /* virtually limit buffer length for romloader, since responses
+ * are shorter and vary in length */
+
+ switch (dnload.romload_state) {
+ case WAITING_PARAM_ACK:
+ buf_used_len = 4; /* ">p" + uint16_t len */
+ break;
+ case WAITING_CHECKSUM_ACK:
+ buf_used_len = 3; /* ">c" + uint8_t checksum */
+ break;
+ case FINISHED:
+ buf_used_len = sizeof(buffer);
+ break;
+ default:
+ buf_used_len = 2; /* ">*" */
+ }
+
+ nbytes = handle_buffer(buf_used_len);
+ if (nbytes <= 0)
+ return nbytes;
+
+ switch (dnload.romload_state) {
+ case WAITING_IDENTIFICATION:
+ if (memcmp(buffer, romload_ident_ack,
+ sizeof(romload_ident_ack)))
+ break;
+
+ printf("Received ident ack from phone, sending "
+ "parameter sequence\n");
+ dnload.expect_hdlc = 1;
+ dnload.romload_state = WAITING_PARAM_ACK;
+ rc = write(dnload.serial_fd.fd, romload_param,
+ sizeof(romload_param));
+ /* re-read file */
+ rc = read_file(dnload.filename);
+ if (rc < 0) {
+ fprintf(stderr, "read_file(%s) failed with %d\n",
+ dnload.filename, rc);
+ exit(1);
+ }
+ break;
+ case WAITING_PARAM_ACK:
+ if (memcmp(buffer, romload_param_ack,
+ sizeof(romload_param_ack)))
+ break;
+
+ printf("Received parameter ack from phone, "
+ "starting download\n");
+ serial_set_baudrate(ROMLOAD_DL_BAUDRATE);
+
+ /* using the max blocksize the phone tells us */
+ dnload.block_payload_size = ((buffer[3] << 8) + buffer[2]);
+ printf("Used blocksize for download is %i bytes\n",
+ dnload.block_payload_size);
+ dnload.block_payload_size -= ROMLOAD_BLOCK_HDR_LEN;
+ dnload.romload_state = SENDING_BLOCKS;
+ dnload.block_number = 0;
+ romload_prepare_block();
+ bufptr -= 2;
+ break;
+ case WAITING_BLOCK_ACK:
+ case LAST_BLOCK_SENT:
+ if (!memcmp(buffer, romload_block_ack,
+ sizeof(romload_block_ack))) {
+ printf("Received block ack from phone\n");
+ if (dnload.romload_state == LAST_BLOCK_SENT) {
+ /* send the checksum */
+ uint8_t final_checksum =
+ (~(dnload.romload_dl_checksum) & 0xff);
+ printf("Sending checksum: 0x%02x \n",
+ final_checksum);
+ rc = write(dnload.serial_fd.fd,
+ romload_checksum_cmd,
+ sizeof(romload_checksum_cmd));
+ rc = write(dnload.serial_fd.fd,
+ &final_checksum, 1);
+ dnload.romload_state = WAITING_CHECKSUM_ACK;
+ } else
+ romload_prepare_block();
+ } else if (!memcmp(buffer, romload_block_nack,
+ sizeof(romload_block_nack))) {
+ printf("Received block nack from phone, "
+ "something went wrong, aborting\n");
+ serial_set_baudrate(ROMLOAD_INIT_BAUDRATE);
+ dnload.romload_state = WAITING_IDENTIFICATION;
+ osmo_timer_schedule(&tick_timer, 0, dnload.beacon_interval);
+ }
+ break;
+ case WAITING_CHECKSUM_ACK:
+ if (!memcmp(buffer, romload_checksum_ack,
+ sizeof(romload_checksum_ack))) {
+ printf("Checksum on phone side matches, "
+ "let's branch to your code\n");
+ printf("Branching to 0x%08x\n", ROMLOAD_ADDRESS);
+
+ rc = write(dnload.serial_fd.fd, romload_branch_cmd,
+ sizeof(romload_branch_cmd));
+ rc = write(dnload.serial_fd.fd, &dnload.load_address,
+ sizeof(dnload.load_address));
+ dnload.romload_state = WAITING_BRANCH_ACK;
+ bufptr -= 1;
+ } else if (!memcmp(buffer, romload_checksum_nack,
+ sizeof(romload_checksum_nack))) {
+ printf("Checksum on phone side (0x%02x) doesn't "
+ "match ours, aborting\n", ~buffer[2]);
+ serial_set_baudrate(ROMLOAD_INIT_BAUDRATE);
+ dnload.romload_state = WAITING_IDENTIFICATION;
+ osmo_timer_schedule(&tick_timer, 0, dnload.beacon_interval);
+ bufptr -= 1;
+ }
+ break;
+ case WAITING_BRANCH_ACK:
+ if (!memcmp(buffer, romload_branch_ack,
+ sizeof(romload_branch_ack))) {
+ printf("Received branch ack, your code is running now!\n");
+ dnload.serial_fd.when = BSC_FD_READ;
+ dnload.romload_state = FINISHED;
+ dnload.write_ptr = dnload.data;
+ dnload.expect_hdlc = 1;
+ } else if (!memcmp(buffer, romload_branch_nack,
+ sizeof(romload_branch_nack))) {
+ printf("Received branch nack, aborting\n");
+ serial_set_baudrate(ROMLOAD_INIT_BAUDRATE);
+ dnload.romload_state = WAITING_IDENTIFICATION;
+ osmo_timer_schedule(&tick_timer, 0, dnload.beacon_interval);
+ }
+ break;
+ default:
+ break;
+ }
+
+ bufptr += nbytes;
+ return nbytes;
+}
+
+/* MTK romloader */
+static int handle_read_mtk(void)
+{
+ int rc, nbytes, buf_used_len;
+
+ switch (dnload.mtk_state) {
+ case MTK_WAIT_ADDR_ACK:
+ case MTK_WAIT_SIZE_ACK:
+ case MTK_WAIT_BRANCH_ADDR_ACK:
+ buf_used_len = 4;
+ break;
+ case MTK_FINISHED:
+ buf_used_len = sizeof(buffer);
+ break;
+ default:
+ buf_used_len = 1;
+ }
+
+ nbytes = handle_buffer(buf_used_len);
+ if (nbytes <= 0)
+ return nbytes;
+
+ switch (dnload.mtk_state) {
+ case MTK_INIT_1:
+ if (!(buffer[0] == mtk_init_resp[0]))
+ break;
+ dnload.mtk_state = MTK_INIT_2;
+ printf("Received init magic byte 1\n");
+ rc = write(dnload.serial_fd.fd, &mtk_init_cmd[1], 1);
+ break;
+ case MTK_INIT_2:
+ if (!(buffer[0] == mtk_init_resp[1]))
+ break;
+ dnload.mtk_state = MTK_INIT_3;
+ printf("Received init magic byte 2\n");
+ rc = write(dnload.serial_fd.fd, &mtk_init_cmd[2], 1);
+ break;
+ case MTK_INIT_3:
+ if (!(buffer[0] == mtk_init_resp[2]))
+ break;
+ dnload.mtk_state = MTK_INIT_4;
+ printf("Received init magic byte 3\n");
+ rc = write(dnload.serial_fd.fd, &mtk_init_cmd[3], 1);
+ break;
+ case MTK_INIT_4:
+ if (!(buffer[0] == mtk_init_resp[3]))
+ break;
+ dnload.mtk_state = MTK_WAIT_WRITE_ACK;
+ printf("Received init magic byte 4, requesting write\n");
+ rc = write(dnload.serial_fd.fd, &mtk_command[0], 1);
+ break;
+ case MTK_WAIT_WRITE_ACK:
+ if (!(buffer[0] == mtk_command[0]))
+ break;
+ dnload.mtk_state = MTK_WAIT_ADDR_ACK;
+ printf("Received write ack, sending load address\n");
+
+ rc = write(dnload.serial_fd.fd, &dnload.load_address,
+ sizeof(dnload.load_address));
+ break;
+ case MTK_WAIT_ADDR_ACK:
+ if (memcmp(buffer, dnload.load_address,
+ sizeof(dnload.load_address)))
+ break;
+ printf("Received address ack from phone, sending loadsize\n");
+ /* re-read file */
+ rc = read_file(dnload.filename);
+ if (rc < 0) {
+ fprintf(stderr, "read_file(%s) failed with %d\n",
+ dnload.filename, rc);
+ exit(1);
+ }
+ dnload.block_number = 0;
+ mtk_prepare_block();
+ dnload.mtk_state = MTK_WAIT_SIZE_ACK;
+ rc = write(dnload.serial_fd.fd, &dnload.mtk_send_size,
+ sizeof(dnload.mtk_send_size));
+ break;
+ case MTK_WAIT_SIZE_ACK:
+ if (memcmp(buffer, dnload.mtk_send_size,
+ sizeof(dnload.mtk_send_size)))
+ break;
+ printf("Received size ack\n");
+ dnload.expect_hdlc = 1;
+ dnload.mtk_state = MTK_SENDING_BLOCKS;
+ dnload.serial_fd.when = BSC_FD_READ | BSC_FD_WRITE;
+ bufptr -= 3;
+ break;
+ case MTK_SENDING_BLOCKS:
+ if (!(buffer[0] == dnload.block[dnload.echo_bytecount]))
+ printf("Warning: Byte %i of Block %i doesn't match,"
+ " check your serial connection!\n",
+ dnload.echo_bytecount, dnload.block_number);
+ dnload.echo_bytecount++;
+
+ if ((dnload.echo_bytecount+1) > MTK_BLOCK_SIZE) {
+ if ( dnload.block_number == dnload.block_count) {
+ rc = write(dnload.serial_fd.fd,
+ &mtk_command[3], 1);
+ printf("Sending branch command\n");
+ dnload.expect_hdlc = 0;
+ dnload.mtk_state = MTK_WAIT_BRANCH_CMD_ACK;
+ break;
+ }
+ printf("Received Block %i preparing next block\n",
+ dnload.block_number);
+ mtk_prepare_block();
+ dnload.serial_fd.when = BSC_FD_READ | BSC_FD_WRITE;
+ }
+ break;
+ case MTK_WAIT_BRANCH_CMD_ACK:
+ if (!(buffer[0] == mtk_command[3]))
+ break;
+ dnload.mtk_state = MTK_WAIT_BRANCH_ADDR_ACK;
+ printf("Received branch command ack, sending address\n");
+
+ rc = write(dnload.serial_fd.fd, &dnload.load_address,
+ sizeof(dnload.load_address));
+ break;
+ case MTK_WAIT_BRANCH_ADDR_ACK:
+ if (memcmp(buffer, dnload.load_address,
+ sizeof(dnload.load_address)))
+ break;
+ printf("Received branch address ack, code should run now\n");
+ serial_set_baudrate(MODEM_BAUDRATE);
+ dnload.serial_fd.when = BSC_FD_READ;
+ dnload.mtk_state = MTK_FINISHED;
+ dnload.write_ptr = dnload.data;
+ dnload.expect_hdlc = 1;
+ break;
+ default:
+ break;
+ }
+
+ bufptr += nbytes;
+ return nbytes;
+}
+
+static int serial_read(struct osmo_fd *fd, unsigned int flags)
+{
+ int rc;
+ if (flags & BSC_FD_READ) {
+ switch (dnload.mode) {
+ case MODE_ROMLOAD:
+ rc = handle_read_romload();
+ break;
+ case MODE_MTK:
+ rc = handle_read_mtk();
+ break;
+ default:
+ rc = handle_read();
+ break;
+ }
+ if (rc == 0)
+ exit(2);
+ }
+
+ if (flags & BSC_FD_WRITE) {
+ rc = handle_write();
+ if (rc == 1)
+ dnload.state = WAITING_PROMPT1;
+ }
+ return 0;
+}
+
+static int parse_mode(const char *arg)
+{
+ if (!strcasecmp(arg, "c123"))
+ return MODE_C123;
+ else if (!strcasecmp(arg, "c123xor"))
+ return MODE_C123xor;
+ else if (!strcasecmp(arg, "c140"))
+ return MODE_C140;
+ else if (!strcasecmp(arg, "c140xor"))
+ return MODE_C140xor;
+ else if (!strcasecmp(arg, "c155"))
+ return MODE_C155;
+ else if (!strcasecmp(arg, "romload"))
+ return MODE_ROMLOAD;
+ else if (!strcasecmp(arg, "mtk"))
+ return MODE_MTK;
+
+ return -1;
+}
+
+#define HELP_TEXT \
+ "[ -v | -h ] [ -d [t][r] ] [ -p /dev/ttyXXXX ]\n" \
+ "\t\t [ -s /tmp/osmocom_l2 ]\n" \
+ "\t\t [ -l /tmp/osmocom_loader ]\n" \
+ "\t\t [ -m {c123,c123xor,c140,c140xor,c155,romload,mtk} ]\n" \
+ "\t\t [ -c /to-be-chainloaded-file.bin ]\n" \
+ "\t\t [ -i beacon-interval (mS) ]\n" \
+ "\t\t file.bin\n\n" \
+ "* Open serial port /dev/ttyXXXX (connected to your phone)\n" \
+ "* Perform handshaking with the ramloader in the phone\n" \
+ "* Download file.bin to the attached phone (base address 0x00800100)\n"
+
+static int usage(const char *name)
+{
+ printf("Usage: %s ", name);
+ printf(HELP_TEXT);
+ exit(2);
+}
+
+static int version(const char *name)
+{
+ printf("%s version %s\n", name, PACKAGE_VERSION);
+ exit(2);
+}
+
+static int un_tool_read(struct osmo_fd *fd, unsigned int flags)
+{
+ int rc, c;
+ uint16_t length = 0xffff;
+ uint8_t buf[4096];
+ struct tool_connection *con = (struct tool_connection *)fd->data;
+
+ c = 0;
+ while(c < 2) {
+ rc = read(fd->fd, &buf + c, 2 - c);
+ if(rc == 0) {
+ // disconnect
+ goto close;
+ }
+ if(rc < 0) {
+ if(errno == EAGAIN) {
+ continue;
+ }
+ fprintf(stderr, "Err from socket: %s\n", strerror(errno));
+ goto close;
+ }
+ c += rc;
+ }
+
+ length = ntohs(*(uint16_t*)buf);
+
+ c = 0;
+ while(c < length) {
+ rc = read(fd->fd, &buf + c, length - c);
+ if(rc == 0) {
+ // disconnect
+ goto close;
+ }
+ if(rc < 0) {
+ if(errno == EAGAIN) {
+ continue;
+ }
+ fprintf(stderr, "Err from socket: %s\n", strerror(errno));
+ goto close;
+ }
+ c += rc;
+ }
+
+ hdlc_send_to_phone(con->server->dlci, buf, length);
+
+ return 0;
+close:
+
+ close(fd->fd);
+ osmo_fd_unregister(fd);
+ llist_del(&con->entry);
+ talloc_free(con);
+ return -1;
+}
+
+/* accept a new connection */
+static int tool_accept(struct osmo_fd *fd, unsigned int flags)
+{
+ struct tool_server *srv = (struct tool_server *)fd->data;
+ struct tool_connection *con;
+ struct sockaddr_un un_addr;
+ socklen_t len;
+ int rc;
+
+ len = sizeof(un_addr);
+ rc = accept(fd->fd, (struct sockaddr *) &un_addr, &len);
+ if (rc < 0) {
+ fprintf(stderr, "Failed to accept a new connection.\n");
+ return -1;
+ }
+
+ con = talloc_zero(NULL, struct tool_connection);
+ if (!con) {
+ fprintf(stderr, "Failed to create tool connection.\n");
+ return -1;
+ }
+
+ con->server = srv;
+
+ con->fd.fd = rc;
+ con->fd.when = BSC_FD_READ;
+ con->fd.cb = un_tool_read;
+ con->fd.data = con;
+ if (osmo_fd_register(&con->fd) != 0) {
+ fprintf(stderr, "Failed to register the fd.\n");
+ return -1;
+ }
+
+ llist_add(&con->entry, &srv->connections);
+ return 0;
+}
+
+/*
+ * Register and start a tool server
+ */
+static int register_tool_server(struct tool_server *ts,
+ const char *path,
+ uint8_t dlci)
+{
+ struct osmo_fd *bfd = &ts->bfd;
+ struct sockaddr_un local;
+ unsigned int namelen;
+ int rc;
+
+ bfd->fd = socket(AF_UNIX, SOCK_STREAM, 0);
+
+ if (bfd->fd < 0) {
+ fprintf(stderr, "Failed to create Unix Domain Socket.\n");
+ return -1;
+ }
+
+ local.sun_family = AF_UNIX;
+ strncpy(local.sun_path, path, sizeof(local.sun_path));
+ local.sun_path[sizeof(local.sun_path) - 1] = '\0';
+ unlink(local.sun_path);
+
+ /* we use the same magic that X11 uses in Xtranssock.c for
+ * calculating the proper length of the sockaddr */
+#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
+ local.sun_len = strlen(local.sun_path);
+#endif
+#if defined(BSD44SOCKETS) || defined(SUN_LEN)
+ namelen = SUN_LEN(&local);
+#else
+ namelen = strlen(local.sun_path) +
+ offsetof(struct sockaddr_un, sun_path);
+#endif
+
+ rc = bind(bfd->fd, (struct sockaddr *) &local, namelen);
+ if (rc != 0) {
+ fprintf(stderr, "Failed to bind the unix domain socket. '%s'\n",
+ local.sun_path);
+ return -1;
+ }
+
+ if (listen(bfd->fd, 0) != 0) {
+ fprintf(stderr, "Failed to listen.\n");
+ return -1;
+ }
+
+ bfd->when = BSC_FD_READ;
+ bfd->cb = tool_accept;
+ bfd->data = ts;
+
+ ts->dlci = dlci;
+ INIT_LLIST_HEAD(&ts->connections);
+
+ tool_server_for_dlci[dlci] = ts;
+
+ sercomm_register_rx_cb(dlci, hdlc_tool_cb);
+
+ if (osmo_fd_register(bfd) != 0) {
+ fprintf(stderr, "Failed to register the bfd.\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+extern void hdlc_tpudbg_cb(uint8_t dlci, struct msgb *msg);
+
+void parse_debug(const char *str)
+{
+ while(*str) {
+ switch(*str) {
+ case 't':
+ dnload.dump_tx = 1;
+ break;
+ case 'r':
+ dnload.dump_rx = 1;
+ break;
+ default:
+ printf("Unknown debug flag %c\n", *str);
+ abort();
+ break;
+ }
+ str++;
+ }
+}
+
+int main(int argc, char **argv)
+{
+ int opt, flags;
+ uint32_t tmp_load_address = ROMLOAD_ADDRESS;
+ const char *serial_dev = "/dev/ttyUSB1";
+ const char *layer2_un_path = "/tmp/osmocom_l2";
+ const char *loader_un_path = "/tmp/osmocom_loader";
+
+ dnload.mode = MODE_C123;
+ dnload.chainload_filename = NULL;
+ dnload.beacon_interval = DEFAULT_BEACON_INTERVAL;
+
+ while ((opt = getopt(argc, argv, "d:hl:p:m:c:s:i:v")) != -1) {
+ switch (opt) {
+ case 'p':
+ serial_dev = optarg;
+ break;
+ case 'm':
+ dnload.mode = parse_mode(optarg);
+ if (dnload.mode < 0)
+ usage(argv[0]);
+ break;
+ case 's':
+ layer2_un_path = optarg;
+ break;
+ case 'l':
+ loader_un_path = optarg;
+ break;
+ case 'v':
+ version(argv[0]);
+ break;
+ case 'd':
+ parse_debug(optarg);
+ break;
+ case 'c':
+ dnload.chainload_filename = optarg;
+ break;
+ case 'i':
+ dnload.beacon_interval = atoi(optarg) * 1000;
+ break;
+ case 'h':
+ default:
+ usage(argv[0]);
+ break;
+ }
+ }
+
+ if (argc <= optind) {
+ dnload.filename = NULL;
+ } else {
+ dnload.filename = argv[optind];
+ }
+
+ dnload.serial_fd.fd = serial_init(serial_dev);
+ if (dnload.serial_fd.fd < 0) {
+ fprintf(stderr, "Cannot open serial device %s\n", serial_dev);
+ exit(1);
+ }
+
+ if (osmo_fd_register(&dnload.serial_fd) != 0) {
+ fprintf(stderr, "Failed to register the serial.\n");
+ exit(1);
+ }
+
+ /* Set serial socket to non-blocking mode of operation */
+ flags = fcntl(dnload.serial_fd.fd, F_GETFL);
+ flags |= O_NONBLOCK;
+ fcntl(dnload.serial_fd.fd, F_SETFL, flags);
+
+ dnload.serial_fd.when = BSC_FD_READ;
+ dnload.serial_fd.cb = serial_read;
+
+ /* initialize the HDLC layer */
+ sercomm_init();
+ sercomm_register_rx_cb(SC_DLCI_CONSOLE, hdlc_console_cb);
+ sercomm_register_rx_cb(SC_DLCI_DEBUG, hdlc_tpudbg_cb);
+
+ /* unix domain socket handling */
+ if (register_tool_server(&dnload.layer2_server, layer2_un_path,
+ SC_DLCI_L1A_L23) != 0)
+ exit(1);
+
+ if (register_tool_server(&dnload.loader_server, loader_un_path,
+ SC_DLCI_LOADER) != 0)
+ exit(1);
+
+ /* if in romload mode, start our beacon timer */
+ if (dnload.mode == MODE_ROMLOAD) {
+ tmp_load_address = ROMLOAD_ADDRESS;
+ serial_set_baudrate(ROMLOAD_INIT_BAUDRATE);
+ tick_timer.cb = &beacon_timer_cb;
+ tick_timer.data = &tick_timer;
+ osmo_timer_schedule(&tick_timer, 0, dnload.beacon_interval);
+ }
+ else if (dnload.mode == MODE_MTK) {
+ tmp_load_address = MTK_ADDRESS;
+ serial_set_baudrate(MTK_INIT_BAUDRATE);
+ tick_timer.cb = &mtk_timer_cb;
+ tick_timer.data = &tick_timer;
+ osmo_timer_schedule(&tick_timer, 0, dnload.beacon_interval);
+ }
+
+ dnload.load_address[0] = (tmp_load_address >> 24) & 0xff;
+ dnload.load_address[1] = (tmp_load_address >> 16) & 0xff;
+ dnload.load_address[2] = (tmp_load_address >> 8) & 0xff;
+ dnload.load_address[3] = tmp_load_address & 0xff;
+
+ while (1) {
+ if (osmo_select_main(0) < 0)
+ break;
+ }
+
+ close(dnload.serial_fd.fd);
+
+ exit(0);
+}
diff --git a/Src/osmoconbb/src/host/osmocon/osmoload.c b/Src/osmoconbb/src/host/osmocon/osmoload.c
new file mode 100644
index 0000000..8a0b21c
--- /dev/null
+++ b/Src/osmoconbb/src/host/osmocon/osmoload.c
@@ -0,0 +1,1216 @@
+/* control utility for the Calypso bootloader */
+
+/* (C) 2010 by Ingo Albrecht <prom@berlin.ccc.de>
+ *
+ * All Rights Reserved
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <getopt.h>
+
+#include <arpa/inet.h>
+
+#include <sys/stat.h>
+
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include <osmocom/core/msgb.h>
+#include <osmocom/core/select.h>
+#include <osmocom/core/timer.h>
+#include <osmocom/core/crc16.h>
+
+#include <loader/protocol.h>
+
+#define MSGB_MAX 256
+
+#define MEM_MSG_MAX (MSGB_MAX - 16)
+
+#define DEFAULT_SOCKET "/tmp/osmocom_loader"
+
+static struct osmo_fd connection;
+
+enum {
+ STATE_INIT,
+ STATE_QUERY_PENDING,
+ STATE_DUMP_IN_PROGRESS,
+ STATE_LOAD_IN_PROGRESS,
+ STATE_FLASHRANGE_GET_INFO,
+ STATE_FLASHRANGE_IN_PROGRESS,
+ STATE_PROGRAM_GET_INFO,
+ STATE_PROGRAM_IN_PROGRESS,
+ STATE_DUMPING,
+};
+
+struct flashblock {
+ uint8_t fb_chip;
+ uint32_t fb_offset;
+ uint32_t fb_addr;
+ uint32_t fb_size;
+};
+
+static struct {
+ /* debug flags */
+ unsigned char print_requests;
+ unsigned char print_replies;
+
+ /* quit flag for main loop */
+ unsigned char quit;
+
+ /* main state machine */
+ int state;
+
+ /* pending query command */
+ uint8_t command;
+
+ /* general timeout */
+ struct osmo_timer_list timeout;
+
+ /* binary i/o for firmware images */
+ FILE *binfile;
+ /* buffer containing binfile data */
+ char *binbuf;
+
+ /* memory operation state */
+ uint8_t memchip; /* target chip (for flashes) */
+ uint32_t membase; /* target base address of operation */
+ uint32_t memlen; /* length of entire operation */
+ uint32_t memoff; /* offset for next request */
+ uint16_t memcrc; /* crc for current request */
+ uint16_t memreq; /* length of current request */
+
+ /* array of all flash blocks */
+ uint8_t flashcommand;
+ uint32_t numblocks;
+ struct flashblock blocks[512];
+} osmoload;
+
+static int usage(const char *name)
+{
+ printf("Usage: %s [ -v | -h ] [ -d tr ] [ -m {c123,c155} ] [ -l /tmp/osmocom_loader ] COMMAND ...\n", name);
+
+ puts("\n Memory commands:");
+ puts(" memget <hex-address> <hex-length> - Peek at memory");
+ puts(" memput <hex-address> <hex-bytes> - Poke at memory");
+ puts(" memdump <hex-address> <hex-length> <file>- Dump memory to file");
+ puts(" memload <hex-address> <file> - Load file into memory");
+
+ puts("\n Flash commands:");
+ puts(" finfo - Information about flash chips");
+ puts(" funlock <address> <length> - Unlock flash block");
+ puts(" flock <address> <length> - Lock flash block");
+ puts(" flockdown <address> <length> - Lock down flash block");
+ puts(" fgetlock <address> <length> - Get locking state of block");
+ puts(" ferase <address> <length> - Erase flash range");
+ puts(" fprogram <chip> <address> <file> - Program file into flash");
+
+ puts("\n Execution commands:");
+ puts(" jump <hex-address> - Jump to address");
+ puts(" jumpflash - Jump to flash loader");
+ puts(" jumprom - Jump to rom loader");
+
+ puts("\n Device lifecycle:");
+ puts(" ping - Ping the loader");
+ puts(" reset - Reset device");
+ puts(" off - Power off device");
+
+ puts("\n Debug:");
+ puts(" dump - Dump loader traffic to console");
+
+ exit(2);
+}
+
+static int version(const char *name)
+{
+ //printf("\n%s version %s\n", name, VERSION);
+ exit(2);
+}
+
+static void osmoload_osmo_hexdump(const uint8_t *data, unsigned int len)
+{
+ const uint8_t *bufptr = data;
+ const uint8_t const *endptr = bufptr + len;
+ int n, m, i, hexchr;
+
+ for (n=0; n < len; n+=32, bufptr += 32) {
+ hexchr = 0;
+ for(m = 0; m < 32 && bufptr < endptr; m++, bufptr++) {
+ if((m) && !(m%4)) {
+ putchar(' ');
+ hexchr++;
+ }
+ printf("%02x", *bufptr);
+ hexchr+=2;
+ }
+ bufptr -= m;
+ int n = 71 - hexchr;
+ for(i = 0; i < n; i++) {
+ putchar(' ');
+ }
+
+ putchar(' ');
+
+ for(m = 0; m < 32 && bufptr < endptr; m++, bufptr++) {
+ if(isgraph(*bufptr)) {
+ putchar(*bufptr);
+ } else {
+ putchar('.');
+ }
+ }
+ bufptr -= m;
+
+ putchar('\n');
+ }
+}
+
+static void
+loader_send_request(struct msgb *msg) {
+ int rc;
+ u_int16_t len = htons(msg->len);
+
+ if(osmoload.print_requests) {
+ printf("Sending %d bytes:\n", msg->len);
+ osmoload_osmo_hexdump(msg->data, msg->len);
+ }
+
+ rc = write(connection.fd, &len, sizeof(len));
+ if(rc != sizeof(len)) {
+ fprintf(stderr, "Error writing.\n");
+ exit(2);
+ }
+
+ rc = write(connection.fd, msg->data, msg->len);
+ if(rc != msg->len) {
+ fprintf(stderr, "Error writing.\n");
+ exit(2);
+ }
+}
+
+static void loader_do_memdump(uint16_t crc, void *address, size_t length);
+static void loader_do_memload();
+static void loader_do_fprogram();
+static void loader_do_flashrange(uint8_t cmd, struct msgb *msg, uint8_t chip, uint32_t address, uint32_t status);
+
+static void memop_timeout(void *dummy) {
+ switch(osmoload.state) {
+ case STATE_LOAD_IN_PROGRESS:
+ printf("Timeout. Repeating.");
+ osmoload.memoff -= osmoload.memreq;
+ loader_do_memload();
+ break;
+ default:
+ break;
+ }
+ return;
+}
+
+static void
+loader_parse_flash_info(struct msgb *msg) {
+ uint8_t nchips;
+
+ nchips = msgb_get_u8(msg);
+
+ osmoload.numblocks = 0;
+
+ int chip;
+ for(chip = 0; chip < nchips; chip++) {
+
+ uint32_t address;
+ address = msgb_get_u32(msg);
+
+ uint32_t chipsize;
+ chipsize = msgb_get_u32(msg);
+
+ uint8_t nregions;
+ nregions = msgb_get_u8(msg);
+
+ printf(" chip %d at 0x%8.8x of %d bytes in %d regions\n", chip, address, chipsize, nregions);
+
+ uint32_t curoffset = 0;
+ int region;
+ for(region = 0; region < nregions; region++) {
+ uint16_t blockcount = msgb_get_u32(msg);
+ uint32_t blocksize = msgb_get_u32(msg);
+
+ printf(" region %d with %d blocks of %d bytes each\n", region, blockcount, blocksize);
+
+ int block;
+ for(block = 0; block < blockcount; block++) {
+ osmoload.blocks[osmoload.numblocks].fb_chip = chip;
+ osmoload.blocks[osmoload.numblocks].fb_offset = curoffset;
+ osmoload.blocks[osmoload.numblocks].fb_addr = address + curoffset;
+ osmoload.blocks[osmoload.numblocks].fb_size = blocksize;
+
+ printf(" block %d with %d bytes at 0x%8.8x on chip %d\n",
+ osmoload.numblocks, blocksize, address + curoffset, chip);
+
+ curoffset += blocksize;
+
+ osmoload.numblocks++;
+ }
+ }
+ }
+}
+
+
+static void
+loader_handle_reply(struct msgb *msg) {
+ if(osmoload.print_replies) {
+ printf("Received %d bytes:\n", msg->len);
+ osmoload_osmo_hexdump(msg->data, msg->len);
+ }
+
+ uint8_t cmd = msgb_get_u8(msg);
+
+ uint8_t chip;
+ uint8_t length;
+ uint16_t crc;
+ uint32_t address;
+ uint32_t entrypoint;
+ uint32_t status;
+
+ void *data;
+
+ switch(cmd) {
+ case LOADER_INIT:
+ address = msgb_get_u32(msg);
+ entrypoint = msgb_get_u32(msg);
+ printf("Loader at entry %x has been started, requesting load to %x\n", entrypoint, address);
+ break;
+ case LOADER_PING:
+ case LOADER_RESET:
+ case LOADER_POWEROFF:
+ case LOADER_ENTER_ROM_LOADER:
+ case LOADER_ENTER_FLASH_LOADER:
+ break;
+ case LOADER_MEM_READ:
+ length = msgb_get_u8(msg);
+ crc = msgb_get_u16(msg);
+ address = msgb_get_u32(msg);
+ data = msgb_get(msg, length);
+ break;
+ case LOADER_MEM_WRITE:
+ length = msgb_get_u8(msg);
+ crc = msgb_get_u16(msg);
+ address = msgb_get_u32(msg);
+ break;
+ case LOADER_JUMP:
+ address = msgb_get_u32(msg);
+ break;
+ case LOADER_FLASH_INFO:
+ break;
+ case LOADER_FLASH_GETLOCK:
+ case LOADER_FLASH_ERASE:
+ case LOADER_FLASH_UNLOCK:
+ case LOADER_FLASH_LOCK:
+ case LOADER_FLASH_LOCKDOWN:
+ chip = msgb_get_u8(msg);
+ address = msgb_get_u32(msg);
+ status = msgb_get_u32(msg);
+ break;
+ case LOADER_FLASH_PROGRAM:
+ length = msgb_get_u8(msg);
+ crc = msgb_get_u16(msg);
+ msgb_get_u8(msg); // XXX align
+ chip = msgb_get_u8(msg);
+ address = msgb_get_u32(msg);
+ status = msgb_get_u32(msg);
+ break;
+ default:
+ printf("Received unknown reply %d:\n", cmd);
+ osmoload_osmo_hexdump(msg->data, msg->len);
+ osmoload.quit = 1;
+ return;
+ }
+
+ switch(osmoload.state) {
+ case STATE_QUERY_PENDING:
+ case STATE_DUMPING:
+ switch(cmd) {
+ case LOADER_PING:
+ printf("Received pong.\n");
+ break;
+ case LOADER_RESET:
+ printf("Reset confirmed.\n");
+ break;
+ case LOADER_POWEROFF:
+ printf("Poweroff confirmed.\n");
+ break;
+ case LOADER_ENTER_ROM_LOADER:
+ printf("Jump to ROM loader confirmed.\n");
+ break;
+ case LOADER_ENTER_FLASH_LOADER:
+ printf("Jump to flash loader confirmed.\n");
+ break;
+ case LOADER_MEM_READ:
+ printf("Received memory dump of %d bytes at 0x%x:\n", length, address);
+ osmoload_osmo_hexdump(data, length);
+ break;
+ case LOADER_MEM_WRITE:
+ printf("Confirmed memory write of %d bytes at 0x%x.\n", length, address);
+ break;
+ case LOADER_JUMP:
+ printf("Confirmed jump to 0x%x.\n", address);
+ break;
+ case LOADER_FLASH_ERASE:
+ printf("Confirmed flash erase of chip %d address 0x%8.8x, status %s\n",
+ chip, address, status ? "FAILED" : "ok");
+ break;
+ case LOADER_FLASH_GETLOCK:
+ printf("Lock state of chip %d address 0x%8.8x is %s\n",
+ chip, address, (status == LOADER_FLASH_LOCKED ? "locked"
+ : (status == LOADER_FLASH_LOCKED_DOWN ? "locked down"
+ : (status == LOADER_FLASH_UNLOCKED ? "unlocked"
+ : "UNKNOWN"))));
+ break;
+ case LOADER_FLASH_UNLOCK:
+ printf("Confirmed flash unlock of chip %d address 0x%8.8x, status %s\n",
+ chip, address, status ? "FAILED" : "ok");
+ break;
+ case LOADER_FLASH_LOCK:
+ printf("Confirmed flash lock of chip %d address 0x%8.8x, status %s\n",
+ chip, address, status ? "FAILED" : "ok");
+ break;
+ case LOADER_FLASH_LOCKDOWN:
+ printf("Confirmed flash lockdown of chip %d address 0x%8.8x, status %s\n",
+ chip, address, status ? "FAILED" : "ok");
+ break;
+ case LOADER_FLASH_INFO:
+ loader_parse_flash_info(msg);
+ break;
+ default:
+ break;
+ }
+ if(osmoload.state == STATE_QUERY_PENDING) {
+ if(osmoload.command == cmd) {
+ osmoload.quit = 1;
+ }
+ }
+ break;
+ case STATE_DUMP_IN_PROGRESS:
+ if(cmd == LOADER_MEM_READ) {
+ loader_do_memdump(crc, data, length);
+ }
+ break;
+ case STATE_LOAD_IN_PROGRESS:
+ if(cmd == LOADER_MEM_WRITE) {
+ if(osmoload.memcrc != crc) {
+ osmoload.memoff -= osmoload.memreq;
+ printf("\nbad crc %4.4x (not %4.4x) at offset 0x%8.8x", crc, osmoload.memcrc, osmoload.memoff);
+ } else {
+ putchar('.');
+ }
+ loader_do_memload();
+ }
+ break;
+ case STATE_PROGRAM_GET_INFO:
+ case STATE_PROGRAM_IN_PROGRESS:
+ if(cmd == LOADER_FLASH_PROGRAM) {
+ if(osmoload.memcrc != crc) {
+ osmoload.memoff -= osmoload.memreq;
+ printf("\nbad crc %4.4x (not %4.4x) at offset 0x%8.8x", crc, osmoload.memcrc, osmoload.memoff);
+ } else {
+ putchar('.');
+ }
+ if(((int)status) != 0) {
+ printf("\nstatus %d, aborting\n", status);
+ exit(1);
+ }
+ loader_do_fprogram();
+ }
+ break;
+ case STATE_FLASHRANGE_GET_INFO:
+ case STATE_FLASHRANGE_IN_PROGRESS:
+ loader_do_flashrange(cmd, msg, chip, address, status);
+ break;
+ default:
+ break;
+ }
+
+ fflush(stdout);
+}
+
+static int
+loader_read_cb(struct osmo_fd *fd, unsigned int flags) {
+ struct msgb *msg;
+ u_int16_t len;
+ int rc;
+
+ msg = msgb_alloc(MSGB_MAX, "loader");
+ if (!msg) {
+ fprintf(stderr, "Failed to allocate msg.\n");
+ return -1;
+ }
+
+ rc = read(fd->fd, &len, sizeof(len));
+ if (rc < sizeof(len)) {
+ fprintf(stderr, "Short read. Error.\n");
+ exit(2);
+ }
+
+ if (ntohs(len) > MSGB_MAX) {
+ fprintf(stderr, "Length is too big: %u\n", ntohs(len));
+ msgb_free(msg);
+ return -1;
+ }
+
+ /* blocking read for the poor... we can starve in here... */
+ msg->l2h = msgb_put(msg, ntohs(len));
+ rc = read(fd->fd, msg->l2h, msgb_l2len(msg));
+ if (rc != msgb_l2len(msg)) {
+ fprintf(stderr, "Can not read data: rc: %d errno: %d\n", rc, errno);
+ msgb_free(msg);
+ return -1;
+ }
+
+ loader_handle_reply(msg);
+
+ msgb_free(msg);
+
+ return 0;
+}
+
+static void
+loader_connect(const char *socket_path) {
+ int rc;
+ struct sockaddr_un local;
+ struct osmo_fd *conn = &connection;
+
+ local.sun_family = AF_UNIX;
+ strncpy(local.sun_path, socket_path, sizeof(local.sun_path));
+ local.sun_path[sizeof(local.sun_path) - 1] = '\0';
+
+ conn->fd = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (conn->fd < 0) {
+ fprintf(stderr, "Failed to create unix domain socket.\n");
+ exit(1);
+ }
+
+ rc = connect(conn->fd, (struct sockaddr *) &local,
+ sizeof(local.sun_family) + strlen(local.sun_path));
+ if (rc < 0) {
+ fprintf(stderr, "Failed to connect to '%s'.\n", local.sun_path);
+ exit(1);
+ }
+
+ conn->when = BSC_FD_READ;
+ conn->cb = loader_read_cb;
+ conn->data = NULL;
+
+ if (osmo_fd_register(conn) != 0) {
+ fprintf(stderr, "Failed to register fd.\n");
+ exit(1);
+ }
+}
+
+static void
+loader_send_simple(uint8_t command) {
+ struct msgb *msg = msgb_alloc(MSGB_MAX, "loader");
+ msgb_put_u8(msg, command);
+ loader_send_request(msg);
+ msgb_free(msg);
+
+ osmoload.command = command;
+}
+
+static void
+loader_start_query(uint8_t command) {
+ loader_send_simple(command);
+ osmoload.state = STATE_QUERY_PENDING;
+}
+
+static void
+loader_send_flash_query(uint8_t command, uint8_t chip, uint32_t address) {
+ struct msgb *msg = msgb_alloc(MSGB_MAX, "loader");
+ msgb_put_u8(msg, command);
+ msgb_put_u8(msg, chip);
+ msgb_put_u32(msg, address);
+ loader_send_request(msg);
+ msgb_free(msg);
+
+ osmoload.command = command;
+}
+
+static void
+loader_start_flash_query(uint8_t command, uint8_t chip, uint32_t address) {
+ loader_send_flash_query(command, chip, address);
+ osmoload.state = STATE_QUERY_PENDING;
+}
+
+static void
+loader_start_memget(uint8_t length, uint32_t address) {
+ struct msgb *msg = msgb_alloc(MSGB_MAX, "loader");
+ msgb_put_u8(msg, LOADER_MEM_READ);
+ msgb_put_u8(msg, length);
+ msgb_put_u32(msg, address);
+ loader_send_request(msg);
+ msgb_free(msg);
+
+ osmoload.state = STATE_QUERY_PENDING;
+ osmoload.command = LOADER_MEM_READ;
+}
+
+static void
+loader_start_memput(uint8_t length, uint32_t address, void *data) {
+ struct msgb *msg = msgb_alloc(MSGB_MAX, "loader");
+ msgb_put_u8(msg, LOADER_MEM_WRITE);
+ msgb_put_u8(msg, length);
+ msgb_put_u16(msg, osmo_crc16(0, data, length));
+ msgb_put_u32(msg, address);
+ memcpy(msgb_put(msg, length), data, length);
+ loader_send_request(msg);
+ msgb_free(msg);
+
+ osmoload.state = STATE_QUERY_PENDING;
+ osmoload.command = LOADER_MEM_WRITE;
+}
+
+static void
+loader_start_jump(uint32_t address) {
+ struct msgb *msg = msgb_alloc(MSGB_MAX, "loader");
+ msgb_put_u8(msg, LOADER_JUMP);
+ msgb_put_u32(msg, address);
+ loader_send_request(msg);
+ msgb_free(msg);
+
+ osmoload.state = STATE_QUERY_PENDING;
+ osmoload.command = LOADER_JUMP;
+}
+
+
+static void
+loader_do_memdump(uint16_t crc, void *data, size_t length) {
+ int rc;
+
+ if(data && length) {
+ osmoload.memcrc = osmo_crc16(0, data, length);
+ if(osmoload.memcrc != crc) {
+ osmoload.memoff -= osmoload.memreq;
+ printf("\nbad crc %4.4x (not %4.4x) at offset 0x%8.8x", crc, osmoload.memcrc, osmoload.memoff);
+ } else {
+ putchar('.');
+ }
+
+ memcpy(osmoload.binbuf + osmoload.memoff, data, length);
+ osmoload.memoff += length;
+ }
+
+ uint32_t rembytes = osmoload.memlen - osmoload.memoff;
+
+ if(!rembytes) {
+ puts("done.");
+ osmoload.quit = 1;
+
+ unsigned c = osmoload.memlen;
+ char *p = osmoload.binbuf;
+ while(c) {
+ rc = fwrite(p, 1, c, osmoload.binfile);
+ if(ferror(osmoload.binfile)) {
+ printf("Could not read from file: %s\n", strerror(errno));
+ exit(1);
+ }
+ c -= rc;
+ p += rc;
+ }
+ fclose(osmoload.binfile);
+ osmoload.binfile = NULL;
+
+ free(osmoload.binbuf);
+
+ return;
+ }
+
+ uint8_t reqbytes = (rembytes < MEM_MSG_MAX) ? rembytes : MEM_MSG_MAX;
+
+ struct msgb *msg = msgb_alloc(MSGB_MAX, "loader");
+
+ msgb_put_u8(msg, LOADER_MEM_READ);
+ msgb_put_u8(msg, reqbytes);
+ msgb_put_u32(msg, osmoload.membase + osmoload.memoff);
+ loader_send_request(msg);
+ msgb_free(msg);
+}
+
+static void
+loader_do_memload() {
+ uint32_t rembytes = osmoload.memlen - osmoload.memoff;
+
+ if(!rembytes) {
+ puts("done.");
+ osmoload.quit = 1;
+ return;
+ }
+
+ osmo_timer_schedule(&osmoload.timeout, 0, 500000);
+
+ uint8_t reqbytes = (rembytes < MEM_MSG_MAX) ? rembytes : MEM_MSG_MAX;
+
+ osmoload.memcrc = osmo_crc16(0, (uint8_t *) osmoload.binbuf + osmoload.memoff, reqbytes);
+ osmoload.memreq = reqbytes;
+
+ struct msgb *msg = msgb_alloc(MSGB_MAX, "loader");
+
+ msgb_put_u8(msg, LOADER_MEM_WRITE);
+ msgb_put_u8(msg, reqbytes);
+ msgb_put_u16(msg, osmoload.memcrc);
+ msgb_put_u32(msg, osmoload.membase + osmoload.memoff);
+
+ unsigned char *p = msgb_put(msg, reqbytes);
+ memcpy(p, osmoload.binbuf + osmoload.memoff, reqbytes);
+
+#if 0
+ printf("Sending %u bytes at offset %u to address %x with crc %x\n",
+ reqbytes, osmoload.memoff, osmoload.membase + osmoload.memoff,
+ osmoload.memcrc);
+#endif
+
+ loader_send_request(msg);
+
+ msgb_free(msg);
+
+ osmoload.memoff += reqbytes;
+}
+
+static void
+loader_do_fprogram() {
+ uint32_t rembytes = osmoload.memlen - osmoload.memoff;
+
+ if(!rembytes) {
+ puts("done.");
+ osmoload.quit = 1;
+ return;
+ }
+
+ osmo_timer_schedule(&osmoload.timeout, 0, 10000000);
+
+ uint8_t reqbytes = (rembytes < MEM_MSG_MAX) ? rembytes : MEM_MSG_MAX;
+
+ osmoload.memcrc = osmo_crc16(0, (uint8_t *) osmoload.binbuf + osmoload.memoff, reqbytes);
+ osmoload.memreq = reqbytes;
+
+ struct msgb *msg = msgb_alloc(MSGB_MAX, "loader");
+
+ msgb_put_u8(msg, LOADER_FLASH_PROGRAM);
+ msgb_put_u8(msg, reqbytes);
+ msgb_put_u16(msg, osmoload.memcrc);
+ msgb_put_u8(msg, 0); // XXX: align data to 16bit
+ msgb_put_u8(msg, osmoload.memchip);
+ msgb_put_u32(msg, osmoload.membase + osmoload.memoff);
+
+ unsigned char *p = msgb_put(msg, reqbytes);
+ memcpy(p, osmoload.binbuf + osmoload.memoff, reqbytes);
+
+#if 0
+ printf("Sending %u bytes at offset %u to address %x with crc %x\n",
+ reqbytes, osmoload.memoff, osmoload.membase + osmoload.memoff,
+ osmoload.memcrc);
+#endif
+
+ loader_send_request(msg);
+
+ msgb_free(msg);
+
+ osmoload.memoff += reqbytes;
+}
+
+static void
+loader_start_memdump(uint32_t length, uint32_t address, char *file) {
+ printf("Dumping %u bytes of memory at 0x%x to file %s\n", length, address, file);
+
+ osmoload.binbuf = malloc(length);
+ if(!osmoload.binbuf) {
+ printf("Could not allocate %u bytes for %s.\n", length, file);
+ exit(1);
+ }
+
+ osmoload.binfile = fopen(file, "wb");
+ if(!osmoload.binfile) {
+ printf("Could not open %s: %s\n", file, strerror(errno));
+ exit(1);
+ }
+
+ osmoload.membase = address;
+ osmoload.memlen = length;
+ osmoload.memoff = 0;
+
+ osmoload.state = STATE_DUMP_IN_PROGRESS;
+ loader_do_memdump(0, NULL, 0);
+}
+
+static void
+loader_start_memload(uint32_t address, char *file) {
+ int rc;
+ struct stat st;
+
+ rc = stat(file, &st);
+ if(rc < 0) {
+ printf("Could not stat %s: %s\n", file, strerror(errno));
+ exit(1);
+ }
+
+ uint32_t length = st.st_size;
+
+ printf("Loading %u bytes of memory to address 0x%x from file %s\n", length, address, file);
+
+ osmoload.binbuf = malloc(length);
+ if(!osmoload.binbuf) {
+ printf("Could not allocate %u bytes for %s.\n", length, file);
+ exit(1);
+ }
+
+ osmoload.binfile = fopen(file, "rb");
+ if(!osmoload.binfile) {
+ printf("Could not open %s: %s\n", file, strerror(errno));
+ exit(1);
+ }
+
+ unsigned c = length;
+ char *p = osmoload.binbuf;
+ while(c) {
+ rc = fread(p, 1, c, osmoload.binfile);
+ if(ferror(osmoload.binfile)) {
+ printf("Could not read from file: %s\n", strerror(errno));
+ exit(1);
+ }
+ c -= rc;
+ p += rc;
+ }
+ fclose(osmoload.binfile);
+ osmoload.binfile = NULL;
+
+ osmoload.membase = address;
+ osmoload.memlen = length;
+ osmoload.memoff = 0;
+
+ osmoload.state = STATE_LOAD_IN_PROGRESS;
+ loader_do_memload();
+}
+
+static void
+loader_start_flashrange(uint8_t command, uint32_t address, uint32_t length) {
+ switch(command) {
+ case LOADER_FLASH_ERASE:
+ printf("Erasing %u bytes of flash at 0x%x\n", length, address);
+ break;
+ case LOADER_FLASH_LOCK:
+ printf("Locking %u bytes of flash at 0x%x\n", length, address);
+ break;
+ case LOADER_FLASH_LOCKDOWN:
+ printf("Locking down %u bytes of flash at 0x%x\n", length, address);
+ break;
+ case LOADER_FLASH_UNLOCK:
+ printf("Unlocking %u bytes of flash at 0x%x\n", length, address);
+ break;
+ case LOADER_FLASH_GETLOCK:
+ printf("Getlocking %u bytes of flash at 0x%x\n", length, address);
+ break;
+ default:
+ puts("Unknown range command");
+ abort();
+ break;
+ }
+
+ osmoload.flashcommand = command;
+
+ osmoload.membase = address;
+ osmoload.memlen = length;
+ osmoload.memoff = 0;
+
+ printf(" requesting flash info to determine block layout\n");
+
+ osmoload.state = STATE_FLASHRANGE_GET_INFO;
+
+ loader_send_simple(LOADER_FLASH_INFO);
+}
+
+static void
+loader_do_flashrange(uint8_t cmd, struct msgb *msg, uint8_t chip, uint32_t address, uint32_t status) {
+ switch(osmoload.state) {
+ case STATE_FLASHRANGE_GET_INFO:
+ if(cmd == LOADER_FLASH_INFO) {
+ loader_parse_flash_info(msg);
+ osmoload.state = STATE_FLASHRANGE_IN_PROGRESS;
+ loader_do_flashrange(0, NULL, 0, 0, 0);
+ }
+ break;
+ case STATE_FLASHRANGE_IN_PROGRESS:
+ {
+ if(msg) {
+ if(cmd == osmoload.flashcommand) {
+ if(cmd == LOADER_FLASH_GETLOCK) {
+ printf(" lock state of chip %d address 0x%8.8x is %s\n",
+ chip, address, (status == LOADER_FLASH_LOCKED ? "locked"
+ : (status == LOADER_FLASH_LOCKED_DOWN ? "locked down"
+ : (status == LOADER_FLASH_UNLOCKED ? "unlocked"
+ : "UNKNOWN"))));
+ } else {
+ printf(" confirmed operation on chip %d address 0x%8.8x, status %s\n",
+ chip, address, status ? "FAILED" : "ok");
+ }
+ } else {
+ break;
+ }
+ }
+
+ uint32_t addr = osmoload.membase + osmoload.memoff;
+
+ if(osmoload.memoff >= osmoload.memlen) {
+ puts(" operation done");
+ osmoload.quit = 1;
+ break;
+ }
+
+ uint8_t found = 0;
+ int i;
+ for(i = 0; i < osmoload.numblocks; i++) {
+ struct flashblock *b = &osmoload.blocks[i];
+ if(b->fb_addr == addr) {
+ loader_send_flash_query(osmoload.flashcommand, b->fb_chip, b->fb_offset);
+ osmoload.memoff += b->fb_size;
+ found = 1;
+ break;
+ }
+ }
+ if(!found) {
+ puts("Oops!? Block not found?"); // XXX message
+ abort();
+ }
+ }
+ break;
+ }
+}
+
+static void
+loader_start_fprogram(uint8_t chip, uint32_t address, char *file) {
+ int rc;
+ struct stat st;
+
+ rc = stat(file, &st);
+ if(rc < 0) {
+ printf("Could not stat %s: %s\n", file, strerror(errno));
+ exit(1);
+ }
+
+ uint32_t length = st.st_size;
+
+ printf("Loading %u bytes of memory at 0x%x in chip %d from file %s\n", length, address, chip, file);
+
+ osmoload.binbuf = malloc(length);
+ if(!osmoload.binbuf) {
+ printf("Could not allocate %u bytes for %s.\n", length, file);
+ exit(1);
+ }
+
+ osmoload.binfile = fopen(file, "rb");
+ if(!osmoload.binfile) {
+ printf("Could not open %s: %s\n", file, strerror(errno));
+ exit(1);
+ }
+
+ unsigned c = length;
+ char *p = osmoload.binbuf;
+ while(c) {
+ rc = fread(p, 1, c, osmoload.binfile);
+ if(ferror(osmoload.binfile)) {
+ printf("Could not read from file: %s\n", strerror(errno));
+ exit(1);
+ }
+ c -= rc;
+ p += rc;
+ }
+ fclose(osmoload.binfile);
+ osmoload.binfile = NULL;
+
+ osmoload.memchip = chip;
+ osmoload.membase = address;
+ osmoload.memlen = length;
+ osmoload.memoff = 0;
+
+ osmoload.state = STATE_PROGRAM_IN_PROGRESS;
+
+ loader_do_fprogram();
+}
+
+static void
+query_timeout(void *dummy) {
+ puts("Query timed out.");
+ exit(2);
+}
+
+static void
+loader_command(char *name, int cmdc, char **cmdv) {
+ if(!cmdc) {
+ usage(name);
+ }
+
+ char *cmd = cmdv[0];
+
+ char buf[MEM_MSG_MAX];
+ memset(buf, 23, sizeof(buf));
+
+ if(!strcmp(cmd, "dump")) {
+ osmoload.state = STATE_DUMPING;
+ } else if(!strcmp(cmd, "ping")) {
+ loader_start_query(LOADER_PING);
+ } else if(!strcmp(cmd, "off")) {
+ loader_start_query(LOADER_POWEROFF);
+ } else if(!strcmp(cmd, "reset")) {
+ loader_start_query(LOADER_RESET);
+ } else if(!strcmp(cmd, "jumprom")) {
+ loader_start_query(LOADER_ENTER_ROM_LOADER);
+ } else if(!strcmp(cmd, "jumpflash")) {
+ loader_start_query(LOADER_ENTER_FLASH_LOADER);
+ } else if(!strcmp(cmd, "finfo")) {
+ puts("Requesting flash layout info");
+ loader_start_query(LOADER_FLASH_INFO);
+ } else if(!strcmp(cmd, "memput")) {
+ uint32_t address;
+
+ if(cmdc < 3) {
+ usage(name);
+ }
+
+ address = strtoul(cmdv[1], NULL, 16);
+
+ unsigned int i;
+ char *hex = cmdv[2];
+ if(strlen(hex)&1) {
+ puts("Invalid hex string.");
+ exit(2);
+ }
+ for(i = 0; i <= sizeof(buf) && i < strlen(hex)/2; i++) {
+ if(i >= sizeof(buf)) {
+ puts("Value too long for single message");
+ exit(2);
+ }
+ unsigned int byte;
+ int count = sscanf(hex + i * 2, "%02x", &byte);
+ if(count != 1) {
+ puts("Invalid hex string.");
+ exit(2);
+ }
+ buf[i] = byte & 0xFF;
+ }
+
+ loader_start_memput(i & 0xFF, address, buf);
+ } else if(!strcmp(cmd, "memget")) {
+ uint32_t address;
+ uint8_t length;
+
+ if(cmdc < 3) {
+ usage(name);
+ }
+
+ address = strtoul(cmdv[1], NULL, 16);
+ length = strtoul(cmdv[2], NULL, 16);
+
+ if(length > MEM_MSG_MAX) {
+ puts("Too many bytes");
+ exit(2);
+ }
+
+ loader_start_memget(length, address);
+ } else if(!strcmp(cmd, "jump")) {
+ uint32_t address;
+
+ if(cmdc < 2) {
+ usage(name);
+ }
+
+ address = strtoul(cmdv[1], NULL, 16);
+
+ loader_start_jump(address);
+ } else if(!strcmp(cmd, "memdump")) {
+ uint32_t address;
+ uint32_t length;
+
+ if(cmdc < 4) {
+ usage(name);
+ }
+
+ address = strtoul(cmdv[1], NULL, 16);
+ length = strtoul(cmdv[2], NULL, 16);
+
+ loader_start_memdump(length, address, cmdv[3]);
+ } else if(!strcmp(cmd, "memload")) {
+ uint32_t address;
+
+ if(cmdc < 3) {
+ usage(name);
+ }
+
+ address = strtoul(cmdv[1], NULL, 16);
+
+ loader_start_memload(address, cmdv[2]);
+ } else if(!strcmp(cmd, "fprogram")) {
+ uint8_t chip;
+ uint32_t address;
+
+ if(cmdc < 4) {
+ usage(name);
+ }
+
+ chip = strtoul(cmdv[1], NULL, 10);
+ address = strtoul(cmdv[2], NULL, 16);
+
+ loader_start_fprogram(chip, address, cmdv[3]);
+ } else if(!strcmp(cmd, "ferase")) {
+ uint32_t address;
+ uint32_t length;
+
+ if(cmdc < 3) {
+ usage(name);
+ }
+
+ address = strtoul(cmdv[1], NULL, 16);
+ length = strtoul(cmdv[2], NULL, 16);
+
+ loader_start_flashrange(LOADER_FLASH_ERASE, address, length);
+ } else if(!strcmp(cmd, "flock")) {
+ uint32_t address;
+ uint32_t length;
+
+ if(cmdc < 3) {
+ usage(name);
+ }
+
+ address = strtoul(cmdv[1], NULL, 16);
+ length = strtoul(cmdv[2], NULL, 16);
+
+ loader_start_flashrange(LOADER_FLASH_LOCK, address, length);
+ } else if(!strcmp(cmd, "flockdown")) {
+ uint32_t address;
+ uint32_t length;
+
+ if(cmdc < 3) {
+ usage(name);
+ }
+
+ address = strtoul(cmdv[1], NULL, 16);
+ length = strtoul(cmdv[2], NULL, 16);
+
+ loader_start_flashrange(LOADER_FLASH_LOCKDOWN, address, length);
+ } else if(!strcmp(cmd, "funlock")) {
+ uint32_t address;
+ uint32_t length;
+
+ if(cmdc < 3) {
+ usage(name);
+ }
+
+ address = strtoul(cmdv[1], NULL, 16);
+ length = strtoul(cmdv[2], NULL, 16);
+
+ loader_start_flashrange(LOADER_FLASH_UNLOCK, address, length);
+ } else if(!strcmp(cmd, "fgetlock")) {
+ uint32_t address;
+ uint32_t length;
+
+ if(cmdc < 3) {
+ usage(name);
+ }
+
+ address = strtoul(cmdv[1], NULL, 16);
+ length = strtoul(cmdv[2], NULL, 16);
+
+ loader_start_flashrange(LOADER_FLASH_GETLOCK, address, length);
+ } else if(!strcmp(cmd, "help")) {
+ usage(name);
+ } else {
+ printf("Unknown command '%s'\n", cmd);
+ usage(name);
+ }
+
+ if(osmoload.state == STATE_QUERY_PENDING) {
+ osmoload.timeout.cb = &query_timeout;
+ osmo_timer_schedule(&osmoload.timeout, 0, 5000000);
+ }
+ if(osmoload.state == STATE_LOAD_IN_PROGRESS) {
+ osmoload.timeout.cb = &memop_timeout;
+ }
+
+}
+
+void
+setdebug(const char *name, char c) {
+ switch(c) {
+ case 't':
+ osmoload.print_requests = 1;
+ break;
+ case 'r':
+ osmoload.print_replies = 1;
+ break;
+ default:
+ usage(name);
+ break;
+ }
+}
+
+int
+main(int argc, char **argv) {
+ int opt;
+ char *loader_un_path = "/tmp/osmocom_loader";
+ const char *debugopt;
+
+ while((opt = getopt(argc, argv, "d:hl:m:v")) != -1) {
+ switch(opt) {
+ case 'd':
+ debugopt = optarg;
+ while(*debugopt) {
+ setdebug(argv[0], *debugopt);
+ debugopt++;
+ }
+ break;
+ case 'l':
+ loader_un_path = optarg;
+ break;
+ case 'm':
+ puts("model selection not implemented");
+ exit(2);
+ break;
+ case 'v':
+ version(argv[0]);
+ break;
+ case 'h':
+ default:
+ usage(argv[0]);
+ break;
+ }
+ }
+
+ osmoload.quit = 0;
+
+ loader_connect(loader_un_path);
+
+ loader_command(argv[0], argc - optind, argv + optind);
+
+ while(!osmoload.quit) {
+ osmo_select_main(0);
+ }
+
+ if(osmoload.binfile) {
+ fclose(osmoload.binfile);
+ }
+
+ return 0;
+}
diff --git a/Src/osmoconbb/src/host/osmocon/tpu_debug.c b/Src/osmoconbb/src/host/osmocon/tpu_debug.c
new file mode 100644
index 0000000..c9dac90
--- /dev/null
+++ b/Src/osmoconbb/src/host/osmocon/tpu_debug.c
@@ -0,0 +1,138 @@
+/* Calypso TPU debugger, displays and decodes TPU instruction RAM */
+
+/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
+ *
+ * All Rights Reserved
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include <stdint.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <osmocom/core/msgb.h>
+
+/* TPU disassembler begin */
+
+static const char *tpu_instr_name[] = {
+ [0] = "SLEEP",
+ [1] = "AT",
+ [2] = "OFFSET",
+ [3] = "SYNCHRO",
+ [4] = "MOVE",
+ [5] = "WAIT",
+ [6] = "UNDEFINED6",
+ [7] = "UNDEFINED7",
+};
+
+static const char *tpu_addr_name[0x1f] = {
+ [0] = "TSP_CTLR1",
+ [1] = "TSP_CTRL2",
+ [4] = "TSP_TX_1",
+ [3] = "TSP_TX_2",
+ [2] = "TSP_TX_3",
+ [5] = "TSP_TX_4",
+ [6] = "TSPACT_L",
+ [7] = "TSPACT_H",
+ [9] = "TSP_SET1",
+ [0xa] = "TSP_SET2",
+ [0xb] = "TSP_SET3",
+ [0x10] = "DSP_INT_PG",
+ [0x11] = "GAUGING_EN",
+};
+
+static uint8_t tpu_reg_cache[0x1f];
+static uint16_t tpu_qbit;
+
+static void tpu_show_instr(uint16_t tpu)
+{
+ uint16_t instr = tpu >> 13;
+ uint16_t param = tpu & 0x1fff;
+ uint16_t addr, data, bitlen;
+ uint32_t tsp_data;
+
+ tpu_qbit++;
+
+ printf("\t %04u %04x %s ", tpu_qbit, tpu, tpu_instr_name[instr]);
+ switch (instr) {
+ case 0:
+ tpu_qbit = 0;
+ default:
+ break;
+ case 1:
+ tpu_qbit = param;
+ printf("%u ", param);
+ break;
+ case 5:
+ tpu_qbit += param;
+ printf("%u ", param);
+ break;
+ case 2:
+ case 3:
+ printf("%u ", param);
+ break;
+ case 4:
+ addr = param & 0x1f;
+ data = param >> 5;
+ tpu_reg_cache[addr] = data;
+ printf("%10s=0x%04x ", tpu_addr_name[addr], data);
+ switch (addr) {
+ case 0:
+ bitlen = (data & 0x1f) + 1;
+ printf("DEV_IDX=%u, BITLEN=%u ", data >> 5, bitlen);
+ if (bitlen <= 8) {
+ tsp_data = tpu_reg_cache[4];
+ printf(" TSP_DATA=0x%02x ", tsp_data);
+ } else if (bitlen <= 16) {
+ tsp_data = tpu_reg_cache[3];
+ tsp_data |= tpu_reg_cache[4] << 8;
+ printf(" TSP_DATA=0x%04x ", tsp_data);
+ } else if (bitlen <= 24) {
+ tsp_data = tpu_reg_cache[2];
+ tsp_data |= tpu_reg_cache[3] << 8;
+ tsp_data |= tpu_reg_cache[4] << 16;
+ printf(" TSP_DATA=0x%06x ", tsp_data);
+ } else {
+ tsp_data = tpu_reg_cache[5];
+ tsp_data |= tpu_reg_cache[2] << 8;
+ tsp_data |= tpu_reg_cache[3] << 16;
+ tsp_data |= tpu_reg_cache[4] << 24;
+ printf(" TSP_DATA=0x%08x ", tsp_data);
+ }
+ break;
+ case 1:
+ if (data & 0x01)
+ printf("READ ");
+ if (data & 0x02)
+ printf("WRITE ");
+ break;
+ }
+ }
+ printf("\n");
+}
+
+void hdlc_tpudbg_cb(uint8_t dlci, struct msgb *msg)
+{
+ uint32_t *fn = (uint32_t *) msg->data;
+ uint16_t *tpu;
+
+ printf("TPU FN %u\n", *fn);
+ for (tpu = (uint16_t *) (msg->data + 4); tpu < (uint16_t *) msg->tail; tpu++)
+ tpu_show_instr(*tpu);
+
+ msgb_free(msg);
+}