├── .gitignore ├── ChangeLog ├── INSTALL ├── LICENSE ├── Makefile.am ├── Makefile.in ├── README ├── SCR-imx-kobs.txt ├── acinclude.m4 ├── aclocal.m4 ├── bootstrap ├── configure ├── configure.in ├── depcomp ├── dot-kobs ├── include ├── Makefile.am ├── Makefile.in ├── config.h.in ├── stamp-vh └── version.h ├── install-sh ├── missing ├── src ├── BootControlBlocks.h ├── Makefile.am ├── Makefile.in ├── aes.h ├── aescrypt.c ├── aeskey.c ├── aesopt.h ├── aestab.c ├── bch.c ├── bch.h ├── bootstream.c ├── bootstream.h ├── dcp_bootstream_ioctl.h ├── main.c ├── mtd.c ├── mtd.h ├── ncb.c ├── plat_boot_config.c ├── plat_boot_config.h ├── rand.h ├── rom_nand_hamming_code_ecc.c ├── rom_nand_hamming_code_ecc.h ├── sha.h └── sha1.c └── version /.gitignore: -------------------------------------------------------------------------------- 1 | # 2 | # NOTE! Don't add files that are generated in specific 3 | # subdirectories here. Add them in the ".gitignore" file 4 | # in that subdirectory instead. 5 | # 6 | # NOTE! Please use 'git ls-files -i --exclude-standard' 7 | # command after changing this file, to see if there are 8 | # any tracked files which get ignored after the change. 9 | # 10 | # Normal rules 11 | # 12 | .* 13 | *.o 14 | *.o.* 15 | *.a 16 | *.s 17 | *.ko 18 | *.so 19 | *.so.dbg 20 | *.mod.c 21 | *.i 22 | *.lst 23 | *.symtypes 24 | *.order 25 | modules.builtin 26 | *.elf 27 | *.bin 28 | *.gz 29 | *.bz2 30 | *.lzma 31 | *.lzo 32 | *.patch 33 | *.gcno 34 | config.log 35 | config.status 36 | src/Makefile 37 | src/kobs-ng 38 | src/tags 39 | Makefile 40 | 41 | # 42 | # Top-level generic files 43 | # 44 | /tags 45 | /TAGS 46 | /linux 47 | /vmlinux 48 | /vmlinuz 49 | /System.map 50 | /Module.markers 51 | /Module.symvers 52 | 53 | 54 | # 55 | # git files that we don't want to ignore even it they are dot-files 56 | # 57 | !.gitignore 58 | !.mailmap 59 | 60 | # 61 | # Generated include files 62 | # 63 | include/config 64 | include/config.h 65 | include/linux/version.h 66 | include/generated 67 | include/autoversion.h 68 | include/stamp-h1 69 | 70 | # stgit generated dirs 71 | patches-* 72 | 73 | # quilt's files 74 | patches 75 | series 76 | 77 | # cscope files 78 | cscope.* 79 | ncscope.* 80 | 81 | # gnu global files 82 | GPATH 83 | GRTAGS 84 | GSYMS 85 | GTAGS 86 | 87 | *.orig 88 | *~ 89 | \#*# 90 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | 0.0.1 2 | 3 | * initial release (mmaptest) 4 | -------------------------------------------------------------------------------- /INSTALL: -------------------------------------------------------------------------------- 1 | 2 | ZEVIO utils installation 3 | ======================== 4 | 5 | Requirements 6 | ------------ 7 | 8 | ... 9 | 10 | Installation 11 | ------------ 12 | 13 | For installation you can use these commands: 14 | 15 | ./configure 16 | make install 17 | 18 | If ./configure command complain that alsa-lib package isn't installed, 19 | please, check if --prefix option is same for alsa-lib and alsa-utils 20 | package. The configure script from alsa-utils package probably cannot find 21 | header file asoundlib.h in $prefix/include/alsa directory (usually in 22 | /usr/include/alsa directory). 23 | 24 | Compilation from SVN sources 25 | --------------------------- 26 | 27 | You need also GNU packages autoconf and automake installed in your system. 28 | 29 | For compilation you can use these commands: 30 | 31 | aclocal 32 | autoheader 33 | automake --foreign --copy --add-missing 34 | autoconf 35 | ./configure 36 | make 37 | 38 | The included svncompile script does this job for you. 39 | 40 | Note: Some automake packages have missing aclocal program. Use newer version 41 | in the case. 42 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | INCLUDES=-I$(top_srcdir)/include 2 | SUBDIRS= include src 3 | EXTRA_DIST= config.rpath mkinstalldirs ChangeLog INSTALL TODO README configure bootstrap depcomp 4 | AUTOMAKE_OPTIONS=foreign 5 | 6 | rpm: dist 7 | $(MAKE) -C $@ 8 | 9 | dist-hook: 10 | -chmod -R a+r $(distdir) 11 | @if ! test -z "$(AMTAR)"; then \ 12 | $(AMTAR) --create --verbose --file=- $(distdir) | bzip2 -c -9 > $(distdir).tar.bz2 ; \ 13 | else \ 14 | $(TAR) --create --verbose --file=- $(distdir) | bzip2 -c -9 > $(distdir).tar.bz2 ; \ 15 | fi 16 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | imx-kobs version 5.5 GPL-2.0 2 | 3 | If you want to compile it in cross environment. 4 | Please try the following : 5 | #./configure CC=arm-none-linux-gnueabihf-gcc --host=arm-none-linux-gnueabihf-gcc 6 | -------------------------------------------------------------------------------- /SCR-imx-kobs.txt: -------------------------------------------------------------------------------- 1 | Package: imx-kobs.git 2 | Outgoing License: GPL-2.0 3 | License File: COPYING 4 | Type of Content: source 5 | Description and comments: NAND boot writer 6 | Release Location: https://github.com/NXPmicro/imx-kobs -b master 7 | Origin: NXP (GPL-2.0) 8 | AES (BSD-3-Clause) - http://brgladman.org/oldsite/AES/ 9 | Secure Hash Algorithm Implementation (Public Domain) - http://www.nic.funet.fi/pub/crypt/hash/sha/ 10 | IGEP Linux Kernel (GPL-2.0+) - http://www.igep.es/ 11 | Generic binary BCH encoding/decoding library - Parrot S.A. (GPL-2.0) - https://lwn.net/Articles/426856/ 12 | -------------------------------------------------------------------------------- /acinclude.m4: -------------------------------------------------------------------------------- 1 | AC_DEFUN([SAVE_KOBS_NG_VERSION], [ 2 | KOBS_NG_VERSION=$VERSION 3 | echo $VERSION > $srcdir/version 4 | AC_DEFINE_UNQUOTED(VERSION, "$KOBS_NG_VERSION", [KOBS NG version]) 5 | AC_SUBST(KOBS_NG_VERSION) 6 | KOBS_NG_MAJOR=`echo $VERSION | cut -d . -f 1` 7 | AC_SUBST(KOBS_NG_MAJOR) 8 | KOBS_NG_MINOR=`echo $VERSION | cut -d . -f 2` 9 | AC_SUBST(KOBS_NG_MINOR) 10 | KOBS_NG_SUBMINOR=`echo $VERSION | cut -d . -f 3 | sed -e 's/pre[[0-9]]*//g'` 11 | AC_SUBST(KOBS_NG_SUBMINOR) 12 | ]) 13 | -------------------------------------------------------------------------------- /bootstrap: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | aclocal 3 | autoheader 4 | automake --foreign --copy --add-missing 5 | touch depcomp # for older automake 6 | autoconf 7 | export CFLAGS='-O2 -Wall -pipe -g' 8 | echo "CFLAGS=$CFLAGS" 9 | echo "./configure $@" 10 | ./configure $@ || exit 1 11 | unset CFLAGS 12 | #if [ -z "$CVSCOMPILE_NO_MAKE" ]; then 13 | # make 14 | #fi 15 | -------------------------------------------------------------------------------- /configure.in: -------------------------------------------------------------------------------- 1 | dnl Process this file with autoconf to produce a configure script. 2 | AC_PREREQ(2.59) 3 | AC_INIT(src/main.c) 4 | AC_PREFIX_DEFAULT(/usr/local) 5 | AM_INIT_AUTOMAKE(kobs-ng, 0.0.1) 6 | 7 | dnl Checks for programs. 8 | 9 | dnl try to gues cross-compiler if not set 10 | if test "x$target" != "x$host" -a -z "`echo $CC | grep -e '-gcc'`"; 11 | then 12 | AC_MSG_CHECKING(for cross-compiler) 13 | 14 | which ${program_prefix}gcc >/dev/null 2>&1 && CC=${program_prefix}gcc 15 | which ${target_cpu}-${target_os}-gcc >/dev/null 2>&1 \ 16 | && CC=${target_cpu}-${target-os}-gcc 17 | which ${target_cpu}-${target_vendor}-${target_os}-gcc >/dev/null 2>&1 \ 18 | && CC=${target_cpu}-${target_vendor}-${target_os}-gcc 19 | 20 | AC_MSG_RESULT($CC) 21 | fi 22 | 23 | AC_PROG_CC 24 | dnl AC_PROG_CXX 25 | AC_PROG_INSTALL 26 | AC_PROG_LN_S 27 | 28 | dnl Checks for header files. 29 | AC_HEADER_STDC 30 | 31 | test "x$prefix" = xNONE && prefix=$ac_default_prefix 32 | 33 | eval dir="$datadir" 34 | case "$dir" in 35 | /*) ;; 36 | *) dir="$prefix/share" 37 | esac 38 | 39 | AM_CONFIG_HEADER(include/config.h) 40 | 41 | dnl Checks for typedefs, structures, and compiler characteristics. 42 | AC_C_CONST 43 | AC_C_INLINE 44 | AC_HEADER_TIME 45 | 46 | dnl Checks for library functions. 47 | AC_PROG_GCC_TRADITIONAL 48 | 49 | dnl Enable largefile support 50 | AC_SYS_LARGEFILE 51 | 52 | dnl big endian? 53 | AC_C_BIGENDIAN 54 | 55 | dnl Some machine CFLAGS tweaking 56 | common_CFLAGS="" 57 | machine_CFLAGS="" 58 | cpu_CFLAGS="" 59 | AC_MSG_CHECKING([machine/cpu specific CFLAGS]) 60 | if test "$ac_cv_c_compiler_gnu" = yes; then 61 | common_CFLAGS="-Wall" 62 | case $ac_cv_target_cpu in 63 | *) 64 | ;; 65 | esac 66 | 67 | case $ac_cv_target_cpu in 68 | *) 69 | ;; 70 | esac 71 | fi 72 | 73 | AC_MSG_RESULT($common_CFLAGS $machine_CFLAGS $cpu_CFLAGS) 74 | 75 | CFLAGS="$CFLAGS $common_CFLAGS $machine_CFLAGS $cpu_CFLAGS" 76 | 77 | SAVE_KOBS_NG_VERSION 78 | 79 | AC_OUTPUT( 80 | Makefile 81 | include/Makefile 82 | src/Makefile) 83 | -------------------------------------------------------------------------------- /depcomp: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | # depcomp - compile a program generating dependencies as side-effects 3 | 4 | scriptversion=2005-07-09.11 5 | 6 | # Copyright (C) 1999, 2000, 2003, 2004, 2005 Free Software Foundation, Inc. 7 | 8 | # This program is free software; you can redistribute it and/or modify 9 | # it under the terms of the GNU General Public License as published by 10 | # the Free Software Foundation; either version 2, or (at your option) 11 | # any later version. 12 | 13 | # This program is distributed in the hope that it will be useful, 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | # GNU General Public License for more details. 17 | 18 | # You should have received a copy of the GNU General Public License 19 | # along with this program; if not, write to the Free Software 20 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 21 | # 02110-1301, USA. 22 | 23 | # As a special exception to the GNU General Public License, if you 24 | # distribute this file as part of a program that contains a 25 | # configuration script generated by Autoconf, you may include it under 26 | # the same distribution terms that you use for the rest of that program. 27 | 28 | # Originally written by Alexandre Oliva . 29 | 30 | case $1 in 31 | '') 32 | echo "$0: No command. Try \`$0 --help' for more information." 1>&2 33 | exit 1; 34 | ;; 35 | -h | --h*) 36 | cat <<\EOF 37 | Usage: depcomp [--help] [--version] PROGRAM [ARGS] 38 | 39 | Run PROGRAMS ARGS to compile a file, generating dependencies 40 | as side-effects. 41 | 42 | Environment variables: 43 | depmode Dependency tracking mode. 44 | source Source file read by `PROGRAMS ARGS'. 45 | object Object file output by `PROGRAMS ARGS'. 46 | DEPDIR directory where to store dependencies. 47 | depfile Dependency file to output. 48 | tmpdepfile Temporary file to use when outputing dependencies. 49 | libtool Whether libtool is used (yes/no). 50 | 51 | Report bugs to . 52 | EOF 53 | exit $? 54 | ;; 55 | -v | --v*) 56 | echo "depcomp $scriptversion" 57 | exit $? 58 | ;; 59 | esac 60 | 61 | if test -z "$depmode" || test -z "$source" || test -z "$object"; then 62 | echo "depcomp: Variables source, object and depmode must be set" 1>&2 63 | exit 1 64 | fi 65 | 66 | # Dependencies for sub/bar.o or sub/bar.obj go into sub/.deps/bar.Po. 67 | depfile=${depfile-`echo "$object" | 68 | sed 's|[^\\/]*$|'${DEPDIR-.deps}'/&|;s|\.\([^.]*\)$|.P\1|;s|Pobj$|Po|'`} 69 | tmpdepfile=${tmpdepfile-`echo "$depfile" | sed 's/\.\([^.]*\)$/.T\1/'`} 70 | 71 | rm -f "$tmpdepfile" 72 | 73 | # Some modes work just like other modes, but use different flags. We 74 | # parameterize here, but still list the modes in the big case below, 75 | # to make depend.m4 easier to write. Note that we *cannot* use a case 76 | # here, because this file can only contain one case statement. 77 | if test "$depmode" = hp; then 78 | # HP compiler uses -M and no extra arg. 79 | gccflag=-M 80 | depmode=gcc 81 | fi 82 | 83 | if test "$depmode" = dashXmstdout; then 84 | # This is just like dashmstdout with a different argument. 85 | dashmflag=-xM 86 | depmode=dashmstdout 87 | fi 88 | 89 | case "$depmode" in 90 | gcc3) 91 | ## gcc 3 implements dependency tracking that does exactly what 92 | ## we want. Yay! Note: for some reason libtool 1.4 doesn't like 93 | ## it if -MD -MP comes after the -MF stuff. Hmm. 94 | "$@" -MT "$object" -MD -MP -MF "$tmpdepfile" 95 | stat=$? 96 | if test $stat -eq 0; then : 97 | else 98 | rm -f "$tmpdepfile" 99 | exit $stat 100 | fi 101 | mv "$tmpdepfile" "$depfile" 102 | ;; 103 | 104 | gcc) 105 | ## There are various ways to get dependency output from gcc. Here's 106 | ## why we pick this rather obscure method: 107 | ## - Don't want to use -MD because we'd like the dependencies to end 108 | ## up in a subdir. Having to rename by hand is ugly. 109 | ## (We might end up doing this anyway to support other compilers.) 110 | ## - The DEPENDENCIES_OUTPUT environment variable makes gcc act like 111 | ## -MM, not -M (despite what the docs say). 112 | ## - Using -M directly means running the compiler twice (even worse 113 | ## than renaming). 114 | if test -z "$gccflag"; then 115 | gccflag=-MD, 116 | fi 117 | "$@" -Wp,"$gccflag$tmpdepfile" 118 | stat=$? 119 | if test $stat -eq 0; then : 120 | else 121 | rm -f "$tmpdepfile" 122 | exit $stat 123 | fi 124 | rm -f "$depfile" 125 | echo "$object : \\" > "$depfile" 126 | alpha=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 127 | ## The second -e expression handles DOS-style file names with drive letters. 128 | sed -e 's/^[^:]*: / /' \ 129 | -e 's/^['$alpha']:\/[^:]*: / /' < "$tmpdepfile" >> "$depfile" 130 | ## This next piece of magic avoids the `deleted header file' problem. 131 | ## The problem is that when a header file which appears in a .P file 132 | ## is deleted, the dependency causes make to die (because there is 133 | ## typically no way to rebuild the header). We avoid this by adding 134 | ## dummy dependencies for each header file. Too bad gcc doesn't do 135 | ## this for us directly. 136 | tr ' ' ' 137 | ' < "$tmpdepfile" | 138 | ## Some versions of gcc put a space before the `:'. On the theory 139 | ## that the space means something, we add a space to the output as 140 | ## well. 141 | ## Some versions of the HPUX 10.20 sed can't process this invocation 142 | ## correctly. Breaking it into two sed invocations is a workaround. 143 | sed -e 's/^\\$//' -e '/^$/d' -e '/:$/d' | sed -e 's/$/ :/' >> "$depfile" 144 | rm -f "$tmpdepfile" 145 | ;; 146 | 147 | hp) 148 | # This case exists only to let depend.m4 do its work. It works by 149 | # looking at the text of this script. This case will never be run, 150 | # since it is checked for above. 151 | exit 1 152 | ;; 153 | 154 | sgi) 155 | if test "$libtool" = yes; then 156 | "$@" "-Wp,-MDupdate,$tmpdepfile" 157 | else 158 | "$@" -MDupdate "$tmpdepfile" 159 | fi 160 | stat=$? 161 | if test $stat -eq 0; then : 162 | else 163 | rm -f "$tmpdepfile" 164 | exit $stat 165 | fi 166 | rm -f "$depfile" 167 | 168 | if test -f "$tmpdepfile"; then # yes, the sourcefile depend on other files 169 | echo "$object : \\" > "$depfile" 170 | 171 | # Clip off the initial element (the dependent). Don't try to be 172 | # clever and replace this with sed code, as IRIX sed won't handle 173 | # lines with more than a fixed number of characters (4096 in 174 | # IRIX 6.2 sed, 8192 in IRIX 6.5). We also remove comment lines; 175 | # the IRIX cc adds comments like `#:fec' to the end of the 176 | # dependency line. 177 | tr ' ' ' 178 | ' < "$tmpdepfile" \ 179 | | sed -e 's/^.*\.o://' -e 's/#.*$//' -e '/^$/ d' | \ 180 | tr ' 181 | ' ' ' >> $depfile 182 | echo >> $depfile 183 | 184 | # The second pass generates a dummy entry for each header file. 185 | tr ' ' ' 186 | ' < "$tmpdepfile" \ 187 | | sed -e 's/^.*\.o://' -e 's/#.*$//' -e '/^$/ d' -e 's/$/:/' \ 188 | >> $depfile 189 | else 190 | # The sourcefile does not contain any dependencies, so just 191 | # store a dummy comment line, to avoid errors with the Makefile 192 | # "include basename.Plo" scheme. 193 | echo "#dummy" > "$depfile" 194 | fi 195 | rm -f "$tmpdepfile" 196 | ;; 197 | 198 | aix) 199 | # The C for AIX Compiler uses -M and outputs the dependencies 200 | # in a .u file. In older versions, this file always lives in the 201 | # current directory. Also, the AIX compiler puts `$object:' at the 202 | # start of each line; $object doesn't have directory information. 203 | # Version 6 uses the directory in both cases. 204 | stripped=`echo "$object" | sed 's/\(.*\)\..*$/\1/'` 205 | tmpdepfile="$stripped.u" 206 | if test "$libtool" = yes; then 207 | "$@" -Wc,-M 208 | else 209 | "$@" -M 210 | fi 211 | stat=$? 212 | 213 | if test -f "$tmpdepfile"; then : 214 | else 215 | stripped=`echo "$stripped" | sed 's,^.*/,,'` 216 | tmpdepfile="$stripped.u" 217 | fi 218 | 219 | if test $stat -eq 0; then : 220 | else 221 | rm -f "$tmpdepfile" 222 | exit $stat 223 | fi 224 | 225 | if test -f "$tmpdepfile"; then 226 | outname="$stripped.o" 227 | # Each line is of the form `foo.o: dependent.h'. 228 | # Do two passes, one to just change these to 229 | # `$object: dependent.h' and one to simply `dependent.h:'. 230 | sed -e "s,^$outname:,$object :," < "$tmpdepfile" > "$depfile" 231 | sed -e "s,^$outname: \(.*\)$,\1:," < "$tmpdepfile" >> "$depfile" 232 | else 233 | # The sourcefile does not contain any dependencies, so just 234 | # store a dummy comment line, to avoid errors with the Makefile 235 | # "include basename.Plo" scheme. 236 | echo "#dummy" > "$depfile" 237 | fi 238 | rm -f "$tmpdepfile" 239 | ;; 240 | 241 | icc) 242 | # Intel's C compiler understands `-MD -MF file'. However on 243 | # icc -MD -MF foo.d -c -o sub/foo.o sub/foo.c 244 | # ICC 7.0 will fill foo.d with something like 245 | # foo.o: sub/foo.c 246 | # foo.o: sub/foo.h 247 | # which is wrong. We want: 248 | # sub/foo.o: sub/foo.c 249 | # sub/foo.o: sub/foo.h 250 | # sub/foo.c: 251 | # sub/foo.h: 252 | # ICC 7.1 will output 253 | # foo.o: sub/foo.c sub/foo.h 254 | # and will wrap long lines using \ : 255 | # foo.o: sub/foo.c ... \ 256 | # sub/foo.h ... \ 257 | # ... 258 | 259 | "$@" -MD -MF "$tmpdepfile" 260 | stat=$? 261 | if test $stat -eq 0; then : 262 | else 263 | rm -f "$tmpdepfile" 264 | exit $stat 265 | fi 266 | rm -f "$depfile" 267 | # Each line is of the form `foo.o: dependent.h', 268 | # or `foo.o: dep1.h dep2.h \', or ` dep3.h dep4.h \'. 269 | # Do two passes, one to just change these to 270 | # `$object: dependent.h' and one to simply `dependent.h:'. 271 | sed "s,^[^:]*:,$object :," < "$tmpdepfile" > "$depfile" 272 | # Some versions of the HPUX 10.20 sed can't process this invocation 273 | # correctly. Breaking it into two sed invocations is a workaround. 274 | sed 's,^[^:]*: \(.*\)$,\1,;s/^\\$//;/^$/d;/:$/d' < "$tmpdepfile" | 275 | sed -e 's/$/ :/' >> "$depfile" 276 | rm -f "$tmpdepfile" 277 | ;; 278 | 279 | tru64) 280 | # The Tru64 compiler uses -MD to generate dependencies as a side 281 | # effect. `cc -MD -o foo.o ...' puts the dependencies into `foo.o.d'. 282 | # At least on Alpha/Redhat 6.1, Compaq CCC V6.2-504 seems to put 283 | # dependencies in `foo.d' instead, so we check for that too. 284 | # Subdirectories are respected. 285 | dir=`echo "$object" | sed -e 's|/[^/]*$|/|'` 286 | test "x$dir" = "x$object" && dir= 287 | base=`echo "$object" | sed -e 's|^.*/||' -e 's/\.o$//' -e 's/\.lo$//'` 288 | 289 | if test "$libtool" = yes; then 290 | # With Tru64 cc, shared objects can also be used to make a 291 | # static library. This mecanism is used in libtool 1.4 series to 292 | # handle both shared and static libraries in a single compilation. 293 | # With libtool 1.4, dependencies were output in $dir.libs/$base.lo.d. 294 | # 295 | # With libtool 1.5 this exception was removed, and libtool now 296 | # generates 2 separate objects for the 2 libraries. These two 297 | # compilations output dependencies in in $dir.libs/$base.o.d and 298 | # in $dir$base.o.d. We have to check for both files, because 299 | # one of the two compilations can be disabled. We should prefer 300 | # $dir$base.o.d over $dir.libs/$base.o.d because the latter is 301 | # automatically cleaned when .libs/ is deleted, while ignoring 302 | # the former would cause a distcleancheck panic. 303 | tmpdepfile1=$dir.libs/$base.lo.d # libtool 1.4 304 | tmpdepfile2=$dir$base.o.d # libtool 1.5 305 | tmpdepfile3=$dir.libs/$base.o.d # libtool 1.5 306 | tmpdepfile4=$dir.libs/$base.d # Compaq CCC V6.2-504 307 | "$@" -Wc,-MD 308 | else 309 | tmpdepfile1=$dir$base.o.d 310 | tmpdepfile2=$dir$base.d 311 | tmpdepfile3=$dir$base.d 312 | tmpdepfile4=$dir$base.d 313 | "$@" -MD 314 | fi 315 | 316 | stat=$? 317 | if test $stat -eq 0; then : 318 | else 319 | rm -f "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3" "$tmpdepfile4" 320 | exit $stat 321 | fi 322 | 323 | for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3" "$tmpdepfile4" 324 | do 325 | test -f "$tmpdepfile" && break 326 | done 327 | if test -f "$tmpdepfile"; then 328 | sed -e "s,^.*\.[a-z]*:,$object:," < "$tmpdepfile" > "$depfile" 329 | # That's a tab and a space in the []. 330 | sed -e 's,^.*\.[a-z]*:[ ]*,,' -e 's,$,:,' < "$tmpdepfile" >> "$depfile" 331 | else 332 | echo "#dummy" > "$depfile" 333 | fi 334 | rm -f "$tmpdepfile" 335 | ;; 336 | 337 | #nosideeffect) 338 | # This comment above is used by automake to tell side-effect 339 | # dependency tracking mechanisms from slower ones. 340 | 341 | dashmstdout) 342 | # Important note: in order to support this mode, a compiler *must* 343 | # always write the preprocessed file to stdout, regardless of -o. 344 | "$@" || exit $? 345 | 346 | # Remove the call to Libtool. 347 | if test "$libtool" = yes; then 348 | while test $1 != '--mode=compile'; do 349 | shift 350 | done 351 | shift 352 | fi 353 | 354 | # Remove `-o $object'. 355 | IFS=" " 356 | for arg 357 | do 358 | case $arg in 359 | -o) 360 | shift 361 | ;; 362 | $object) 363 | shift 364 | ;; 365 | *) 366 | set fnord "$@" "$arg" 367 | shift # fnord 368 | shift # $arg 369 | ;; 370 | esac 371 | done 372 | 373 | test -z "$dashmflag" && dashmflag=-M 374 | # Require at least two characters before searching for `:' 375 | # in the target name. This is to cope with DOS-style filenames: 376 | # a dependency such as `c:/foo/bar' could be seen as target `c' otherwise. 377 | "$@" $dashmflag | 378 | sed 's:^[ ]*[^: ][^:][^:]*\:[ ]*:'"$object"'\: :' > "$tmpdepfile" 379 | rm -f "$depfile" 380 | cat < "$tmpdepfile" > "$depfile" 381 | tr ' ' ' 382 | ' < "$tmpdepfile" | \ 383 | ## Some versions of the HPUX 10.20 sed can't process this invocation 384 | ## correctly. Breaking it into two sed invocations is a workaround. 385 | sed -e 's/^\\$//' -e '/^$/d' -e '/:$/d' | sed -e 's/$/ :/' >> "$depfile" 386 | rm -f "$tmpdepfile" 387 | ;; 388 | 389 | dashXmstdout) 390 | # This case only exists to satisfy depend.m4. It is never actually 391 | # run, as this mode is specially recognized in the preamble. 392 | exit 1 393 | ;; 394 | 395 | makedepend) 396 | "$@" || exit $? 397 | # Remove any Libtool call 398 | if test "$libtool" = yes; then 399 | while test $1 != '--mode=compile'; do 400 | shift 401 | done 402 | shift 403 | fi 404 | # X makedepend 405 | shift 406 | cleared=no 407 | for arg in "$@"; do 408 | case $cleared in 409 | no) 410 | set ""; shift 411 | cleared=yes ;; 412 | esac 413 | case "$arg" in 414 | -D*|-I*) 415 | set fnord "$@" "$arg"; shift ;; 416 | # Strip any option that makedepend may not understand. Remove 417 | # the object too, otherwise makedepend will parse it as a source file. 418 | -*|$object) 419 | ;; 420 | *) 421 | set fnord "$@" "$arg"; shift ;; 422 | esac 423 | done 424 | obj_suffix="`echo $object | sed 's/^.*\././'`" 425 | touch "$tmpdepfile" 426 | ${MAKEDEPEND-makedepend} -o"$obj_suffix" -f"$tmpdepfile" "$@" 427 | rm -f "$depfile" 428 | cat < "$tmpdepfile" > "$depfile" 429 | sed '1,2d' "$tmpdepfile" | tr ' ' ' 430 | ' | \ 431 | ## Some versions of the HPUX 10.20 sed can't process this invocation 432 | ## correctly. Breaking it into two sed invocations is a workaround. 433 | sed -e 's/^\\$//' -e '/^$/d' -e '/:$/d' | sed -e 's/$/ :/' >> "$depfile" 434 | rm -f "$tmpdepfile" "$tmpdepfile".bak 435 | ;; 436 | 437 | cpp) 438 | # Important note: in order to support this mode, a compiler *must* 439 | # always write the preprocessed file to stdout. 440 | "$@" || exit $? 441 | 442 | # Remove the call to Libtool. 443 | if test "$libtool" = yes; then 444 | while test $1 != '--mode=compile'; do 445 | shift 446 | done 447 | shift 448 | fi 449 | 450 | # Remove `-o $object'. 451 | IFS=" " 452 | for arg 453 | do 454 | case $arg in 455 | -o) 456 | shift 457 | ;; 458 | $object) 459 | shift 460 | ;; 461 | *) 462 | set fnord "$@" "$arg" 463 | shift # fnord 464 | shift # $arg 465 | ;; 466 | esac 467 | done 468 | 469 | "$@" -E | 470 | sed -n -e '/^# [0-9][0-9]* "\([^"]*\)".*/ s:: \1 \\:p' \ 471 | -e '/^#line [0-9][0-9]* "\([^"]*\)".*/ s:: \1 \\:p' | 472 | sed '$ s: \\$::' > "$tmpdepfile" 473 | rm -f "$depfile" 474 | echo "$object : \\" > "$depfile" 475 | cat < "$tmpdepfile" >> "$depfile" 476 | sed < "$tmpdepfile" '/^$/d;s/^ //;s/ \\$//;s/$/ :/' >> "$depfile" 477 | rm -f "$tmpdepfile" 478 | ;; 479 | 480 | msvisualcpp) 481 | # Important note: in order to support this mode, a compiler *must* 482 | # always write the preprocessed file to stdout, regardless of -o, 483 | # because we must use -o when running libtool. 484 | "$@" || exit $? 485 | IFS=" " 486 | for arg 487 | do 488 | case "$arg" in 489 | "-Gm"|"/Gm"|"-Gi"|"/Gi"|"-ZI"|"/ZI") 490 | set fnord "$@" 491 | shift 492 | shift 493 | ;; 494 | *) 495 | set fnord "$@" "$arg" 496 | shift 497 | shift 498 | ;; 499 | esac 500 | done 501 | "$@" -E | 502 | sed -n '/^#line [0-9][0-9]* "\([^"]*\)"/ s::echo "`cygpath -u \\"\1\\"`":p' | sort | uniq > "$tmpdepfile" 503 | rm -f "$depfile" 504 | echo "$object : \\" > "$depfile" 505 | . "$tmpdepfile" | sed 's% %\\ %g' | sed -n '/^\(.*\)$/ s:: \1 \\:p' >> "$depfile" 506 | echo " " >> "$depfile" 507 | . "$tmpdepfile" | sed 's% %\\ %g' | sed -n '/^\(.*\)$/ s::\1\::p' >> "$depfile" 508 | rm -f "$tmpdepfile" 509 | ;; 510 | 511 | none) 512 | exec "$@" 513 | ;; 514 | 515 | *) 516 | echo "Unknown depmode $depmode" 1>&2 517 | exit 1 518 | ;; 519 | esac 520 | 521 | exit 0 522 | 523 | # Local Variables: 524 | # mode: shell-script 525 | # sh-indentation: 2 526 | # eval: (add-hook 'write-file-hooks 'time-stamp) 527 | # time-stamp-start: "scriptversion=" 528 | # time-stamp-format: "%:y-%02m-%02d.%02H" 529 | # time-stamp-end: "$" 530 | # End: 531 | -------------------------------------------------------------------------------- /dot-kobs: -------------------------------------------------------------------------------- 1 | # 2 | # The sample configuration file for kobs 3 | # 4 | chip_0_device_path=/dev/mtd0 5 | 6 | search_exponent = 2 7 | data_setup_time = 80 8 | data_hold_time = 60 9 | address_setup_time = 25 10 | data_sample_time = 6 11 | row_address_size = 3 12 | column_address_size = 2 13 | 14 | ncb_version = 3 15 | 16 | boot_stream_1_address = 0 17 | boot_stream_2_address = 0x1500000 18 | 19 | -------------------------------------------------------------------------------- /include/Makefile.am: -------------------------------------------------------------------------------- 1 | noinst_HEADERS=version.h 2 | 3 | version.h: stamp-vh 4 | @: 5 | 6 | all: config.h 7 | echo "const char *git_sha = \""`git rev-parse HEAD`"\";" > ../include/autoversion.h 8 | 9 | stamp-vh: $(top_builddir)/configure.in 10 | @echo "/*" > ver.tmp 11 | @echo " * version.h" >> ver.tmp 12 | @echo " */" >> ver.tmp 13 | @echo "" >> ver.tmp 14 | @echo "#define KOBS_NG_MAJOR $(KOBS_NG_MAJOR)" >> ver.tmp 15 | @echo "#define KOBS_NG_MINOR $(KOBS_NG_MINOR)" >> ver.tmp 16 | @echo "#define KOBS_NG_SUBMINOR $(KOBS_NG_SUBMINOR)" >> ver.tmp 17 | @echo "#define KOBS_NG_VERSION ((KOBS_NG_MAJOR<<16)|\\" >> ver.tmp 18 | @echo " (KOBS_NG_MINOR<<8)|\\" >> ver.tmp 19 | @echo " KOBS_NG_SUBMINOR)" >> ver.tmp 20 | @echo "#define KOBS_NG_VERSION_STR \"$(KOBS_NG_VERSION)\"" >> ver.tmp 21 | @echo >> ver.tmp 22 | @cmp -s version.h ver.tmp \ 23 | || (echo "Updating version.h"; \ 24 | cp ver.tmp version.h; \ 25 | echo timestamp > stamp-vh) 26 | -@rm -f ver.tmp 27 | 28 | INCLUDES=-I$(top_srcdir)/include 29 | -------------------------------------------------------------------------------- /include/Makefile.in: -------------------------------------------------------------------------------- 1 | # Makefile.in generated by automake 1.10.1 from Makefile.am. 2 | # @configure_input@ 3 | 4 | # Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 5 | # 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. 6 | # This Makefile.in is free software; the Free Software Foundation 7 | # gives unlimited permission to copy and/or distribute it, 8 | # with or without modifications, as long as this notice is preserved. 9 | 10 | # This program is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY, to the extent permitted by law; without 12 | # even the implied warranty of MERCHANTABILITY or FITNESS FOR A 13 | # PARTICULAR PURPOSE. 14 | 15 | @SET_MAKE@ 16 | 17 | VPATH = @srcdir@ 18 | pkgdatadir = $(datadir)/@PACKAGE@ 19 | pkglibdir = $(libdir)/@PACKAGE@ 20 | pkgincludedir = $(includedir)/@PACKAGE@ 21 | am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd 22 | install_sh_DATA = $(install_sh) -c -m 644 23 | install_sh_PROGRAM = $(install_sh) -c 24 | install_sh_SCRIPT = $(install_sh) -c 25 | INSTALL_HEADER = $(INSTALL_DATA) 26 | transform = $(program_transform_name) 27 | NORMAL_INSTALL = : 28 | PRE_INSTALL = : 29 | POST_INSTALL = : 30 | NORMAL_UNINSTALL = : 31 | PRE_UNINSTALL = : 32 | POST_UNINSTALL = : 33 | subdir = include 34 | DIST_COMMON = $(noinst_HEADERS) $(srcdir)/Makefile.am \ 35 | $(srcdir)/Makefile.in $(srcdir)/config.h.in 36 | ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 37 | am__aclocal_m4_deps = $(top_srcdir)/acinclude.m4 \ 38 | $(top_srcdir)/configure.in 39 | am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ 40 | $(ACLOCAL_M4) 41 | mkinstalldirs = $(install_sh) -d 42 | CONFIG_HEADER = config.h 43 | CONFIG_CLEAN_FILES = 44 | SOURCES = 45 | DIST_SOURCES = 46 | HEADERS = $(noinst_HEADERS) 47 | ETAGS = etags 48 | CTAGS = ctags 49 | DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) 50 | ACLOCAL = @ACLOCAL@ 51 | AMTAR = @AMTAR@ 52 | AUTOCONF = @AUTOCONF@ 53 | AUTOHEADER = @AUTOHEADER@ 54 | AUTOMAKE = @AUTOMAKE@ 55 | AWK = @AWK@ 56 | CC = @CC@ 57 | CCDEPMODE = @CCDEPMODE@ 58 | CFLAGS = @CFLAGS@ 59 | CPP = @CPP@ 60 | CPPFLAGS = @CPPFLAGS@ 61 | CYGPATH_W = @CYGPATH_W@ 62 | DEFS = @DEFS@ 63 | DEPDIR = @DEPDIR@ 64 | ECHO_C = @ECHO_C@ 65 | ECHO_N = @ECHO_N@ 66 | ECHO_T = @ECHO_T@ 67 | EGREP = @EGREP@ 68 | EXEEXT = @EXEEXT@ 69 | GREP = @GREP@ 70 | INSTALL = @INSTALL@ 71 | INSTALL_DATA = @INSTALL_DATA@ 72 | INSTALL_PROGRAM = @INSTALL_PROGRAM@ 73 | INSTALL_SCRIPT = @INSTALL_SCRIPT@ 74 | INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ 75 | KOBS_NG_MAJOR = @KOBS_NG_MAJOR@ 76 | KOBS_NG_MINOR = @KOBS_NG_MINOR@ 77 | KOBS_NG_SUBMINOR = @KOBS_NG_SUBMINOR@ 78 | KOBS_NG_VERSION = @KOBS_NG_VERSION@ 79 | LDFLAGS = @LDFLAGS@ 80 | LIBOBJS = @LIBOBJS@ 81 | LIBS = @LIBS@ 82 | LN_S = @LN_S@ 83 | LTLIBOBJS = @LTLIBOBJS@ 84 | MAKEINFO = @MAKEINFO@ 85 | MKDIR_P = @MKDIR_P@ 86 | OBJEXT = @OBJEXT@ 87 | PACKAGE = @PACKAGE@ 88 | PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@ 89 | PACKAGE_NAME = @PACKAGE_NAME@ 90 | PACKAGE_STRING = @PACKAGE_STRING@ 91 | PACKAGE_TARNAME = @PACKAGE_TARNAME@ 92 | PACKAGE_VERSION = @PACKAGE_VERSION@ 93 | PATH_SEPARATOR = @PATH_SEPARATOR@ 94 | SET_MAKE = @SET_MAKE@ 95 | SHELL = @SHELL@ 96 | STRIP = @STRIP@ 97 | VERSION = @VERSION@ 98 | abs_builddir = @abs_builddir@ 99 | abs_srcdir = @abs_srcdir@ 100 | abs_top_builddir = @abs_top_builddir@ 101 | abs_top_srcdir = @abs_top_srcdir@ 102 | ac_ct_CC = @ac_ct_CC@ 103 | am__include = @am__include@ 104 | am__leading_dot = @am__leading_dot@ 105 | am__quote = @am__quote@ 106 | am__tar = @am__tar@ 107 | am__untar = @am__untar@ 108 | bindir = @bindir@ 109 | build_alias = @build_alias@ 110 | builddir = @builddir@ 111 | datadir = @datadir@ 112 | datarootdir = @datarootdir@ 113 | docdir = @docdir@ 114 | dvidir = @dvidir@ 115 | exec_prefix = @exec_prefix@ 116 | host_alias = @host_alias@ 117 | htmldir = @htmldir@ 118 | includedir = @includedir@ 119 | infodir = @infodir@ 120 | install_sh = @install_sh@ 121 | libdir = @libdir@ 122 | libexecdir = @libexecdir@ 123 | localedir = @localedir@ 124 | localstatedir = @localstatedir@ 125 | mandir = @mandir@ 126 | mkdir_p = @mkdir_p@ 127 | oldincludedir = @oldincludedir@ 128 | pdfdir = @pdfdir@ 129 | prefix = @prefix@ 130 | program_transform_name = @program_transform_name@ 131 | psdir = @psdir@ 132 | sbindir = @sbindir@ 133 | sharedstatedir = @sharedstatedir@ 134 | srcdir = @srcdir@ 135 | sysconfdir = @sysconfdir@ 136 | target_alias = @target_alias@ 137 | top_builddir = @top_builddir@ 138 | top_srcdir = @top_srcdir@ 139 | noinst_HEADERS = version.h 140 | INCLUDES = -I$(top_srcdir)/include 141 | all: config.h 142 | echo "const char *git_sha = \""`git rev-parse HEAD`"\";" > ../include/autoversion.h 143 | $(MAKE) $(AM_MAKEFLAGS) all-am 144 | 145 | .SUFFIXES: 146 | $(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps) 147 | @for dep in $?; do \ 148 | case '$(am__configure_deps)' in \ 149 | *$$dep*) \ 150 | cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh \ 151 | && exit 0; \ 152 | exit 1;; \ 153 | esac; \ 154 | done; \ 155 | echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign include/Makefile'; \ 156 | cd $(top_srcdir) && \ 157 | $(AUTOMAKE) --foreign include/Makefile 158 | .PRECIOUS: Makefile 159 | Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status 160 | @case '$?' in \ 161 | *config.status*) \ 162 | cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \ 163 | *) \ 164 | echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)'; \ 165 | cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe);; \ 166 | esac; 167 | 168 | $(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) 169 | cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh 170 | 171 | $(top_srcdir)/configure: $(am__configure_deps) 172 | cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh 173 | $(ACLOCAL_M4): $(am__aclocal_m4_deps) 174 | cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh 175 | 176 | config.h: stamp-h1 177 | @if test ! -f $@; then \ 178 | rm -f stamp-h1; \ 179 | $(MAKE) $(AM_MAKEFLAGS) stamp-h1; \ 180 | else :; fi 181 | 182 | stamp-h1: $(srcdir)/config.h.in $(top_builddir)/config.status 183 | @rm -f stamp-h1 184 | cd $(top_builddir) && $(SHELL) ./config.status include/config.h 185 | $(srcdir)/config.h.in: $(am__configure_deps) 186 | cd $(top_srcdir) && $(AUTOHEADER) 187 | rm -f stamp-h1 188 | touch $@ 189 | 190 | distclean-hdr: 191 | -rm -f config.h stamp-h1 192 | 193 | ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES) 194 | list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ 195 | unique=`for i in $$list; do \ 196 | if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ 197 | done | \ 198 | $(AWK) '{ files[$$0] = 1; nonemtpy = 1; } \ 199 | END { if (nonempty) { for (i in files) print i; }; }'`; \ 200 | mkid -fID $$unique 201 | tags: TAGS 202 | 203 | TAGS: $(HEADERS) $(SOURCES) config.h.in $(TAGS_DEPENDENCIES) \ 204 | $(TAGS_FILES) $(LISP) 205 | tags=; \ 206 | here=`pwd`; \ 207 | list='$(SOURCES) $(HEADERS) config.h.in $(LISP) $(TAGS_FILES)'; \ 208 | unique=`for i in $$list; do \ 209 | if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ 210 | done | \ 211 | $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ 212 | END { if (nonempty) { for (i in files) print i; }; }'`; \ 213 | if test -z "$(ETAGS_ARGS)$$tags$$unique"; then :; else \ 214 | test -n "$$unique" || unique=$$empty_fix; \ 215 | $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ 216 | $$tags $$unique; \ 217 | fi 218 | ctags: CTAGS 219 | CTAGS: $(HEADERS) $(SOURCES) config.h.in $(TAGS_DEPENDENCIES) \ 220 | $(TAGS_FILES) $(LISP) 221 | tags=; \ 222 | list='$(SOURCES) $(HEADERS) config.h.in $(LISP) $(TAGS_FILES)'; \ 223 | unique=`for i in $$list; do \ 224 | if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ 225 | done | \ 226 | $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ 227 | END { if (nonempty) { for (i in files) print i; }; }'`; \ 228 | test -z "$(CTAGS_ARGS)$$tags$$unique" \ 229 | || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \ 230 | $$tags $$unique 231 | 232 | GTAGS: 233 | here=`$(am__cd) $(top_builddir) && pwd` \ 234 | && cd $(top_srcdir) \ 235 | && gtags -i $(GTAGS_ARGS) $$here 236 | 237 | distclean-tags: 238 | -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags 239 | 240 | distdir: $(DISTFILES) 241 | @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ 242 | topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ 243 | list='$(DISTFILES)'; \ 244 | dist_files=`for file in $$list; do echo $$file; done | \ 245 | sed -e "s|^$$srcdirstrip/||;t" \ 246 | -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \ 247 | case $$dist_files in \ 248 | */*) $(MKDIR_P) `echo "$$dist_files" | \ 249 | sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \ 250 | sort -u` ;; \ 251 | esac; \ 252 | for file in $$dist_files; do \ 253 | if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ 254 | if test -d $$d/$$file; then \ 255 | dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \ 256 | if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ 257 | cp -pR $(srcdir)/$$file $(distdir)$$dir || exit 1; \ 258 | fi; \ 259 | cp -pR $$d/$$file $(distdir)$$dir || exit 1; \ 260 | else \ 261 | test -f $(distdir)/$$file \ 262 | || cp -p $$d/$$file $(distdir)/$$file \ 263 | || exit 1; \ 264 | fi; \ 265 | done 266 | check-am: all-am 267 | check: check-am 268 | all-am: Makefile $(HEADERS) config.h 269 | installdirs: 270 | install: install-am 271 | install-exec: install-exec-am 272 | install-data: install-data-am 273 | uninstall: uninstall-am 274 | 275 | install-am: all-am 276 | @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am 277 | 278 | installcheck: installcheck-am 279 | install-strip: 280 | $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ 281 | install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ 282 | `test -z '$(STRIP)' || \ 283 | echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install 284 | mostlyclean-generic: 285 | 286 | clean-generic: 287 | 288 | distclean-generic: 289 | -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) 290 | 291 | maintainer-clean-generic: 292 | @echo "This command is intended for maintainers to use" 293 | @echo "it deletes files that may require special tools to rebuild." 294 | clean: clean-am 295 | 296 | clean-am: clean-generic mostlyclean-am 297 | 298 | distclean: distclean-am 299 | -rm -f Makefile 300 | distclean-am: clean-am distclean-generic distclean-hdr distclean-tags 301 | 302 | dvi: dvi-am 303 | 304 | dvi-am: 305 | 306 | html: html-am 307 | 308 | info: info-am 309 | 310 | info-am: 311 | 312 | install-data-am: 313 | 314 | install-dvi: install-dvi-am 315 | 316 | install-exec-am: 317 | 318 | install-html: install-html-am 319 | 320 | install-info: install-info-am 321 | 322 | install-man: 323 | 324 | install-pdf: install-pdf-am 325 | 326 | install-ps: install-ps-am 327 | 328 | installcheck-am: 329 | 330 | maintainer-clean: maintainer-clean-am 331 | -rm -f Makefile 332 | maintainer-clean-am: distclean-am maintainer-clean-generic 333 | 334 | mostlyclean: mostlyclean-am 335 | 336 | mostlyclean-am: mostlyclean-generic 337 | 338 | pdf: pdf-am 339 | 340 | pdf-am: 341 | 342 | ps: ps-am 343 | 344 | ps-am: 345 | 346 | uninstall-am: 347 | 348 | .MAKE: install-am install-strip 349 | 350 | .PHONY: CTAGS GTAGS all all-am check check-am clean clean-generic \ 351 | ctags distclean distclean-generic distclean-hdr distclean-tags \ 352 | distdir dvi dvi-am html html-am info info-am install \ 353 | install-am install-data install-data-am install-dvi \ 354 | install-dvi-am install-exec install-exec-am install-html \ 355 | install-html-am install-info install-info-am install-man \ 356 | install-pdf install-pdf-am install-ps install-ps-am \ 357 | install-strip installcheck installcheck-am installdirs \ 358 | maintainer-clean maintainer-clean-generic mostlyclean \ 359 | mostlyclean-generic pdf pdf-am ps ps-am tags uninstall \ 360 | uninstall-am 361 | 362 | 363 | version.h: stamp-vh 364 | @: 365 | 366 | stamp-vh: $(top_builddir)/configure.in 367 | @echo "/*" > ver.tmp 368 | @echo " * version.h" >> ver.tmp 369 | @echo " */" >> ver.tmp 370 | @echo "" >> ver.tmp 371 | @echo "#define KOBS_NG_MAJOR $(KOBS_NG_MAJOR)" >> ver.tmp 372 | @echo "#define KOBS_NG_MINOR $(KOBS_NG_MINOR)" >> ver.tmp 373 | @echo "#define KOBS_NG_SUBMINOR $(KOBS_NG_SUBMINOR)" >> ver.tmp 374 | @echo "#define KOBS_NG_VERSION ((KOBS_NG_MAJOR<<16)|\\" >> ver.tmp 375 | @echo " (KOBS_NG_MINOR<<8)|\\" >> ver.tmp 376 | @echo " KOBS_NG_SUBMINOR)" >> ver.tmp 377 | @echo "#define KOBS_NG_VERSION_STR \"$(KOBS_NG_VERSION)\"" >> ver.tmp 378 | @echo >> ver.tmp 379 | @cmp -s version.h ver.tmp \ 380 | || (echo "Updating version.h"; \ 381 | cp ver.tmp version.h; \ 382 | echo timestamp > stamp-vh) 383 | -@rm -f ver.tmp 384 | # Tell versions [3.59,3.63) of GNU make to not export all variables. 385 | # Otherwise a system limit (for SysV at least) may be exceeded. 386 | .NOEXPORT: 387 | -------------------------------------------------------------------------------- /include/config.h.in: -------------------------------------------------------------------------------- 1 | /* include/config.h.in. Generated from configure.in by autoheader. */ 2 | 3 | /* Define to 1 if you have the header file. */ 4 | #undef HAVE_INTTYPES_H 5 | 6 | /* Define to 1 if you have the header file. */ 7 | #undef HAVE_MEMORY_H 8 | 9 | /* Define to 1 if you have the header file. */ 10 | #undef HAVE_STDINT_H 11 | 12 | /* Define to 1 if you have the header file. */ 13 | #undef HAVE_STDLIB_H 14 | 15 | /* Define to 1 if you have the header file. */ 16 | #undef HAVE_STRINGS_H 17 | 18 | /* Define to 1 if you have the header file. */ 19 | #undef HAVE_STRING_H 20 | 21 | /* Define to 1 if you have the header file. */ 22 | #undef HAVE_SYS_STAT_H 23 | 24 | /* Define to 1 if you have the header file. */ 25 | #undef HAVE_SYS_TYPES_H 26 | 27 | /* Define to 1 if you have the header file. */ 28 | #undef HAVE_UNISTD_H 29 | 30 | /* Name of package */ 31 | #undef PACKAGE 32 | 33 | /* Define to the address where bug reports for this package should be sent. */ 34 | #undef PACKAGE_BUGREPORT 35 | 36 | /* Define to the full name of this package. */ 37 | #undef PACKAGE_NAME 38 | 39 | /* Define to the full name and version of this package. */ 40 | #undef PACKAGE_STRING 41 | 42 | /* Define to the one symbol short name of this package. */ 43 | #undef PACKAGE_TARNAME 44 | 45 | /* Define to the version of this package. */ 46 | #undef PACKAGE_VERSION 47 | 48 | /* Define to 1 if you have the ANSI C header files. */ 49 | #undef STDC_HEADERS 50 | 51 | /* Define to 1 if you can safely include both and . */ 52 | #undef TIME_WITH_SYS_TIME 53 | 54 | /* KOBS NG version */ 55 | #undef VERSION 56 | 57 | /* Define to 1 if your processor stores words with the most significant byte 58 | first (like Motorola and SPARC, unlike Intel and VAX). */ 59 | #undef WORDS_BIGENDIAN 60 | 61 | /* Number of bits in a file offset, on hosts where this is settable. */ 62 | #undef _FILE_OFFSET_BITS 63 | 64 | /* Define for large files, on AIX-style hosts. */ 65 | #undef _LARGE_FILES 66 | 67 | /* Define to empty if `const' does not conform to ANSI C. */ 68 | #undef const 69 | 70 | /* Define to `__inline__' or `__inline' if that's what the C compiler 71 | calls it, or to nothing if 'inline' is not supported under any name. */ 72 | #ifndef __cplusplus 73 | #undef inline 74 | #endif 75 | -------------------------------------------------------------------------------- /include/stamp-vh: -------------------------------------------------------------------------------- 1 | timestamp 2 | -------------------------------------------------------------------------------- /include/version.h: -------------------------------------------------------------------------------- 1 | /* 2 | * version.h 3 | */ 4 | 5 | #define KOBS_NG_MAJOR 0 6 | #define KOBS_NG_MINOR 0 7 | #define KOBS_NG_SUBMINOR 1 8 | #define KOBS_NG_VERSION ((KOBS_NG_MAJOR<<16)|\ 9 | (KOBS_NG_MINOR<<8)|\ 10 | KOBS_NG_SUBMINOR) 11 | #define KOBS_NG_VERSION_STR "0.0.1" 12 | 13 | -------------------------------------------------------------------------------- /install-sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # install - install a program, script, or datafile 3 | 4 | scriptversion=2005-05-14.22 5 | 6 | # This originates from X11R5 (mit/util/scripts/install.sh), which was 7 | # later released in X11R6 (xc/config/util/install.sh) with the 8 | # following copyright and license. 9 | # 10 | # Copyright (C) 1994 X Consortium 11 | # 12 | # Permission is hereby granted, free of charge, to any person obtaining a copy 13 | # of this software and associated documentation files (the "Software"), to 14 | # deal in the Software without restriction, including without limitation the 15 | # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 16 | # sell copies of the Software, and to permit persons to whom the Software is 17 | # furnished to do so, subject to the following conditions: 18 | # 19 | # The above copyright notice and this permission notice shall be included in 20 | # all copies or substantial portions of the Software. 21 | # 22 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | # X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 26 | # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 27 | # TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28 | # 29 | # Except as contained in this notice, the name of the X Consortium shall not 30 | # be used in advertising or otherwise to promote the sale, use or other deal- 31 | # ings in this Software without prior written authorization from the X Consor- 32 | # tium. 33 | # 34 | # 35 | # FSF changes to this file are in the public domain. 36 | # 37 | # Calling this script install-sh is preferred over install.sh, to prevent 38 | # `make' implicit rules from creating a file called install from it 39 | # when there is no Makefile. 40 | # 41 | # This script is compatible with the BSD install script, but was written 42 | # from scratch. It can only install one file at a time, a restriction 43 | # shared with many OS's install programs. 44 | 45 | # set DOITPROG to echo to test this script 46 | 47 | # Don't use :- since 4.3BSD and earlier shells don't like it. 48 | doit="${DOITPROG-}" 49 | 50 | # put in absolute paths if you don't have them in your path; or use env. vars. 51 | 52 | mvprog="${MVPROG-mv}" 53 | cpprog="${CPPROG-cp}" 54 | chmodprog="${CHMODPROG-chmod}" 55 | chownprog="${CHOWNPROG-chown}" 56 | chgrpprog="${CHGRPPROG-chgrp}" 57 | stripprog="${STRIPPROG-strip}" 58 | rmprog="${RMPROG-rm}" 59 | mkdirprog="${MKDIRPROG-mkdir}" 60 | 61 | chmodcmd="$chmodprog 0755" 62 | chowncmd= 63 | chgrpcmd= 64 | stripcmd= 65 | rmcmd="$rmprog -f" 66 | mvcmd="$mvprog" 67 | src= 68 | dst= 69 | dir_arg= 70 | dstarg= 71 | no_target_directory= 72 | 73 | usage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE 74 | or: $0 [OPTION]... SRCFILES... DIRECTORY 75 | or: $0 [OPTION]... -t DIRECTORY SRCFILES... 76 | or: $0 [OPTION]... -d DIRECTORIES... 77 | 78 | In the 1st form, copy SRCFILE to DSTFILE. 79 | In the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 80 | In the 4th, create DIRECTORIES. 81 | 82 | Options: 83 | -c (ignored) 84 | -d create directories instead of installing files. 85 | -g GROUP $chgrpprog installed files to GROUP. 86 | -m MODE $chmodprog installed files to MODE. 87 | -o USER $chownprog installed files to USER. 88 | -s $stripprog installed files. 89 | -t DIRECTORY install into DIRECTORY. 90 | -T report an error if DSTFILE is a directory. 91 | --help display this help and exit. 92 | --version display version info and exit. 93 | 94 | Environment variables override the default commands: 95 | CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG 96 | " 97 | 98 | while test -n "$1"; do 99 | case $1 in 100 | -c) shift 101 | continue;; 102 | 103 | -d) dir_arg=true 104 | shift 105 | continue;; 106 | 107 | -g) chgrpcmd="$chgrpprog $2" 108 | shift 109 | shift 110 | continue;; 111 | 112 | --help) echo "$usage"; exit $?;; 113 | 114 | -m) chmodcmd="$chmodprog $2" 115 | shift 116 | shift 117 | continue;; 118 | 119 | -o) chowncmd="$chownprog $2" 120 | shift 121 | shift 122 | continue;; 123 | 124 | -s) stripcmd=$stripprog 125 | shift 126 | continue;; 127 | 128 | -t) dstarg=$2 129 | shift 130 | shift 131 | continue;; 132 | 133 | -T) no_target_directory=true 134 | shift 135 | continue;; 136 | 137 | --version) echo "$0 $scriptversion"; exit $?;; 138 | 139 | *) # When -d is used, all remaining arguments are directories to create. 140 | # When -t is used, the destination is already specified. 141 | test -n "$dir_arg$dstarg" && break 142 | # Otherwise, the last argument is the destination. Remove it from $@. 143 | for arg 144 | do 145 | if test -n "$dstarg"; then 146 | # $@ is not empty: it contains at least $arg. 147 | set fnord "$@" "$dstarg" 148 | shift # fnord 149 | fi 150 | shift # arg 151 | dstarg=$arg 152 | done 153 | break;; 154 | esac 155 | done 156 | 157 | if test -z "$1"; then 158 | if test -z "$dir_arg"; then 159 | echo "$0: no input file specified." >&2 160 | exit 1 161 | fi 162 | # It's OK to call `install-sh -d' without argument. 163 | # This can happen when creating conditional directories. 164 | exit 0 165 | fi 166 | 167 | for src 168 | do 169 | # Protect names starting with `-'. 170 | case $src in 171 | -*) src=./$src ;; 172 | esac 173 | 174 | if test -n "$dir_arg"; then 175 | dst=$src 176 | src= 177 | 178 | if test -d "$dst"; then 179 | mkdircmd=: 180 | chmodcmd= 181 | else 182 | mkdircmd=$mkdirprog 183 | fi 184 | else 185 | # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 186 | # might cause directories to be created, which would be especially bad 187 | # if $src (and thus $dsttmp) contains '*'. 188 | if test ! -f "$src" && test ! -d "$src"; then 189 | echo "$0: $src does not exist." >&2 190 | exit 1 191 | fi 192 | 193 | if test -z "$dstarg"; then 194 | echo "$0: no destination specified." >&2 195 | exit 1 196 | fi 197 | 198 | dst=$dstarg 199 | # Protect names starting with `-'. 200 | case $dst in 201 | -*) dst=./$dst ;; 202 | esac 203 | 204 | # If destination is a directory, append the input filename; won't work 205 | # if double slashes aren't ignored. 206 | if test -d "$dst"; then 207 | if test -n "$no_target_directory"; then 208 | echo "$0: $dstarg: Is a directory" >&2 209 | exit 1 210 | fi 211 | dst=$dst/`basename "$src"` 212 | fi 213 | fi 214 | 215 | # This sed command emulates the dirname command. 216 | dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'` 217 | 218 | # Make sure that the destination directory exists. 219 | 220 | # Skip lots of stat calls in the usual case. 221 | if test ! -d "$dstdir"; then 222 | defaultIFS=' 223 | ' 224 | IFS="${IFS-$defaultIFS}" 225 | 226 | oIFS=$IFS 227 | # Some sh's can't handle IFS=/ for some reason. 228 | IFS='%' 229 | set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` 230 | shift 231 | IFS=$oIFS 232 | 233 | pathcomp= 234 | 235 | while test $# -ne 0 ; do 236 | pathcomp=$pathcomp$1 237 | shift 238 | if test ! -d "$pathcomp"; then 239 | $mkdirprog "$pathcomp" 240 | # mkdir can fail with a `File exist' error in case several 241 | # install-sh are creating the directory concurrently. This 242 | # is OK. 243 | test -d "$pathcomp" || exit 244 | fi 245 | pathcomp=$pathcomp/ 246 | done 247 | fi 248 | 249 | if test -n "$dir_arg"; then 250 | $doit $mkdircmd "$dst" \ 251 | && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \ 252 | && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \ 253 | && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \ 254 | && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; } 255 | 256 | else 257 | dstfile=`basename "$dst"` 258 | 259 | # Make a couple of temp file names in the proper directory. 260 | dsttmp=$dstdir/_inst.$$_ 261 | rmtmp=$dstdir/_rm.$$_ 262 | 263 | # Trap to clean up those temp files at exit. 264 | trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 265 | trap '(exit $?); exit' 1 2 13 15 266 | 267 | # Copy the file name to the temp name. 268 | $doit $cpprog "$src" "$dsttmp" && 269 | 270 | # and set any options; do chmod last to preserve setuid bits. 271 | # 272 | # If any of these fail, we abort the whole thing. If we want to 273 | # ignore errors from any of these, just make sure not to ignore 274 | # errors from the above "$doit $cpprog $src $dsttmp" command. 275 | # 276 | { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \ 277 | && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \ 278 | && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \ 279 | && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } && 280 | 281 | # Now rename the file to the real destination. 282 | { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \ 283 | || { 284 | # The rename failed, perhaps because mv can't rename something else 285 | # to itself, or perhaps because mv is so ancient that it does not 286 | # support -f. 287 | 288 | # Now remove or move aside any old file at destination location. 289 | # We try this two ways since rm can't unlink itself on some 290 | # systems and the destination file might be busy for other 291 | # reasons. In this case, the final cleanup might fail but the new 292 | # file should still install successfully. 293 | { 294 | if test -f "$dstdir/$dstfile"; then 295 | $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \ 296 | || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \ 297 | || { 298 | echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 299 | (exit 1); exit 1 300 | } 301 | else 302 | : 303 | fi 304 | } && 305 | 306 | # Now rename the file to the real destination. 307 | $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" 308 | } 309 | } 310 | fi || { (exit 1); exit 1; } 311 | done 312 | 313 | # The final little trick to "correctly" pass the exit status to the exit trap. 314 | { 315 | (exit 0); exit 0 316 | } 317 | 318 | # Local variables: 319 | # eval: (add-hook 'write-file-hooks 'time-stamp) 320 | # time-stamp-start: "scriptversion=" 321 | # time-stamp-format: "%:y-%02m-%02d.%02H" 322 | # time-stamp-end: "$" 323 | # End: 324 | -------------------------------------------------------------------------------- /missing: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | # Common stub for a few missing GNU programs while installing. 3 | 4 | scriptversion=2005-06-08.21 5 | 6 | # Copyright (C) 1996, 1997, 1999, 2000, 2002, 2003, 2004, 2005 7 | # Free Software Foundation, Inc. 8 | # Originally by Fran,cois Pinard , 1996. 9 | 10 | # This program is free software; you can redistribute it and/or modify 11 | # it under the terms of the GNU General Public License as published by 12 | # the Free Software Foundation; either version 2, or (at your option) 13 | # any later version. 14 | 15 | # This program is distributed in the hope that it will be useful, 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | # GNU General Public License for more details. 19 | 20 | # You should have received a copy of the GNU General Public License 21 | # along with this program; if not, write to the Free Software 22 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 23 | # 02110-1301, USA. 24 | 25 | # As a special exception to the GNU General Public License, if you 26 | # distribute this file as part of a program that contains a 27 | # configuration script generated by Autoconf, you may include it under 28 | # the same distribution terms that you use for the rest of that program. 29 | 30 | if test $# -eq 0; then 31 | echo 1>&2 "Try \`$0 --help' for more information" 32 | exit 1 33 | fi 34 | 35 | run=: 36 | 37 | # In the cases where this matters, `missing' is being run in the 38 | # srcdir already. 39 | if test -f configure.ac; then 40 | configure_ac=configure.ac 41 | else 42 | configure_ac=configure.in 43 | fi 44 | 45 | msg="missing on your system" 46 | 47 | case "$1" in 48 | --run) 49 | # Try to run requested program, and just exit if it succeeds. 50 | run= 51 | shift 52 | "$@" && exit 0 53 | # Exit code 63 means version mismatch. This often happens 54 | # when the user try to use an ancient version of a tool on 55 | # a file that requires a minimum version. In this case we 56 | # we should proceed has if the program had been absent, or 57 | # if --run hadn't been passed. 58 | if test $? = 63; then 59 | run=: 60 | msg="probably too old" 61 | fi 62 | ;; 63 | 64 | -h|--h|--he|--hel|--help) 65 | echo "\ 66 | $0 [OPTION]... PROGRAM [ARGUMENT]... 67 | 68 | Handle \`PROGRAM [ARGUMENT]...' for when PROGRAM is missing, or return an 69 | error status if there is no known handling for PROGRAM. 70 | 71 | Options: 72 | -h, --help display this help and exit 73 | -v, --version output version information and exit 74 | --run try to run the given command, and emulate it if it fails 75 | 76 | Supported PROGRAM values: 77 | aclocal touch file \`aclocal.m4' 78 | autoconf touch file \`configure' 79 | autoheader touch file \`config.h.in' 80 | automake touch all \`Makefile.in' files 81 | bison create \`y.tab.[ch]', if possible, from existing .[ch] 82 | flex create \`lex.yy.c', if possible, from existing .c 83 | help2man touch the output file 84 | lex create \`lex.yy.c', if possible, from existing .c 85 | makeinfo touch the output file 86 | tar try tar, gnutar, gtar, then tar without non-portable flags 87 | yacc create \`y.tab.[ch]', if possible, from existing .[ch] 88 | 89 | Send bug reports to ." 90 | exit $? 91 | ;; 92 | 93 | -v|--v|--ve|--ver|--vers|--versi|--versio|--version) 94 | echo "missing $scriptversion (GNU Automake)" 95 | exit $? 96 | ;; 97 | 98 | -*) 99 | echo 1>&2 "$0: Unknown \`$1' option" 100 | echo 1>&2 "Try \`$0 --help' for more information" 101 | exit 1 102 | ;; 103 | 104 | esac 105 | 106 | # Now exit if we have it, but it failed. Also exit now if we 107 | # don't have it and --version was passed (most likely to detect 108 | # the program). 109 | case "$1" in 110 | lex|yacc) 111 | # Not GNU programs, they don't have --version. 112 | ;; 113 | 114 | tar) 115 | if test -n "$run"; then 116 | echo 1>&2 "ERROR: \`tar' requires --run" 117 | exit 1 118 | elif test "x$2" = "x--version" || test "x$2" = "x--help"; then 119 | exit 1 120 | fi 121 | ;; 122 | 123 | *) 124 | if test -z "$run" && ($1 --version) > /dev/null 2>&1; then 125 | # We have it, but it failed. 126 | exit 1 127 | elif test "x$2" = "x--version" || test "x$2" = "x--help"; then 128 | # Could not run --version or --help. This is probably someone 129 | # running `$TOOL --version' or `$TOOL --help' to check whether 130 | # $TOOL exists and not knowing $TOOL uses missing. 131 | exit 1 132 | fi 133 | ;; 134 | esac 135 | 136 | # If it does not exist, or fails to run (possibly an outdated version), 137 | # try to emulate it. 138 | case "$1" in 139 | aclocal*) 140 | echo 1>&2 "\ 141 | WARNING: \`$1' is $msg. You should only need it if 142 | you modified \`acinclude.m4' or \`${configure_ac}'. You might want 143 | to install the \`Automake' and \`Perl' packages. Grab them from 144 | any GNU archive site." 145 | touch aclocal.m4 146 | ;; 147 | 148 | autoconf) 149 | echo 1>&2 "\ 150 | WARNING: \`$1' is $msg. You should only need it if 151 | you modified \`${configure_ac}'. You might want to install the 152 | \`Autoconf' and \`GNU m4' packages. Grab them from any GNU 153 | archive site." 154 | touch configure 155 | ;; 156 | 157 | autoheader) 158 | echo 1>&2 "\ 159 | WARNING: \`$1' is $msg. You should only need it if 160 | you modified \`acconfig.h' or \`${configure_ac}'. You might want 161 | to install the \`Autoconf' and \`GNU m4' packages. Grab them 162 | from any GNU archive site." 163 | files=`sed -n 's/^[ ]*A[CM]_CONFIG_HEADER(\([^)]*\)).*/\1/p' ${configure_ac}` 164 | test -z "$files" && files="config.h" 165 | touch_files= 166 | for f in $files; do 167 | case "$f" in 168 | *:*) touch_files="$touch_files "`echo "$f" | 169 | sed -e 's/^[^:]*://' -e 's/:.*//'`;; 170 | *) touch_files="$touch_files $f.in";; 171 | esac 172 | done 173 | touch $touch_files 174 | ;; 175 | 176 | automake*) 177 | echo 1>&2 "\ 178 | WARNING: \`$1' is $msg. You should only need it if 179 | you modified \`Makefile.am', \`acinclude.m4' or \`${configure_ac}'. 180 | You might want to install the \`Automake' and \`Perl' packages. 181 | Grab them from any GNU archive site." 182 | find . -type f -name Makefile.am -print | 183 | sed 's/\.am$/.in/' | 184 | while read f; do touch "$f"; done 185 | ;; 186 | 187 | autom4te) 188 | echo 1>&2 "\ 189 | WARNING: \`$1' is needed, but is $msg. 190 | You might have modified some files without having the 191 | proper tools for further handling them. 192 | You can get \`$1' as part of \`Autoconf' from any GNU 193 | archive site." 194 | 195 | file=`echo "$*" | sed -n 's/.*--output[ =]*\([^ ]*\).*/\1/p'` 196 | test -z "$file" && file=`echo "$*" | sed -n 's/.*-o[ ]*\([^ ]*\).*/\1/p'` 197 | if test -f "$file"; then 198 | touch $file 199 | else 200 | test -z "$file" || exec >$file 201 | echo "#! /bin/sh" 202 | echo "# Created by GNU Automake missing as a replacement of" 203 | echo "# $ $@" 204 | echo "exit 0" 205 | chmod +x $file 206 | exit 1 207 | fi 208 | ;; 209 | 210 | bison|yacc) 211 | echo 1>&2 "\ 212 | WARNING: \`$1' $msg. You should only need it if 213 | you modified a \`.y' file. You may need the \`Bison' package 214 | in order for those modifications to take effect. You can get 215 | \`Bison' from any GNU archive site." 216 | rm -f y.tab.c y.tab.h 217 | if [ $# -ne 1 ]; then 218 | eval LASTARG="\${$#}" 219 | case "$LASTARG" in 220 | *.y) 221 | SRCFILE=`echo "$LASTARG" | sed 's/y$/c/'` 222 | if [ -f "$SRCFILE" ]; then 223 | cp "$SRCFILE" y.tab.c 224 | fi 225 | SRCFILE=`echo "$LASTARG" | sed 's/y$/h/'` 226 | if [ -f "$SRCFILE" ]; then 227 | cp "$SRCFILE" y.tab.h 228 | fi 229 | ;; 230 | esac 231 | fi 232 | if [ ! -f y.tab.h ]; then 233 | echo >y.tab.h 234 | fi 235 | if [ ! -f y.tab.c ]; then 236 | echo 'main() { return 0; }' >y.tab.c 237 | fi 238 | ;; 239 | 240 | lex|flex) 241 | echo 1>&2 "\ 242 | WARNING: \`$1' is $msg. You should only need it if 243 | you modified a \`.l' file. You may need the \`Flex' package 244 | in order for those modifications to take effect. You can get 245 | \`Flex' from any GNU archive site." 246 | rm -f lex.yy.c 247 | if [ $# -ne 1 ]; then 248 | eval LASTARG="\${$#}" 249 | case "$LASTARG" in 250 | *.l) 251 | SRCFILE=`echo "$LASTARG" | sed 's/l$/c/'` 252 | if [ -f "$SRCFILE" ]; then 253 | cp "$SRCFILE" lex.yy.c 254 | fi 255 | ;; 256 | esac 257 | fi 258 | if [ ! -f lex.yy.c ]; then 259 | echo 'main() { return 0; }' >lex.yy.c 260 | fi 261 | ;; 262 | 263 | help2man) 264 | echo 1>&2 "\ 265 | WARNING: \`$1' is $msg. You should only need it if 266 | you modified a dependency of a manual page. You may need the 267 | \`Help2man' package in order for those modifications to take 268 | effect. You can get \`Help2man' from any GNU archive site." 269 | 270 | file=`echo "$*" | sed -n 's/.*-o \([^ ]*\).*/\1/p'` 271 | if test -z "$file"; then 272 | file=`echo "$*" | sed -n 's/.*--output=\([^ ]*\).*/\1/p'` 273 | fi 274 | if [ -f "$file" ]; then 275 | touch $file 276 | else 277 | test -z "$file" || exec >$file 278 | echo ".ab help2man is required to generate this page" 279 | exit 1 280 | fi 281 | ;; 282 | 283 | makeinfo) 284 | echo 1>&2 "\ 285 | WARNING: \`$1' is $msg. You should only need it if 286 | you modified a \`.texi' or \`.texinfo' file, or any other file 287 | indirectly affecting the aspect of the manual. The spurious 288 | call might also be the consequence of using a buggy \`make' (AIX, 289 | DU, IRIX). You might want to install the \`Texinfo' package or 290 | the \`GNU make' package. Grab either from any GNU archive site." 291 | # The file to touch is that specified with -o ... 292 | file=`echo "$*" | sed -n 's/.*-o \([^ ]*\).*/\1/p'` 293 | if test -z "$file"; then 294 | # ... or it is the one specified with @setfilename ... 295 | infile=`echo "$*" | sed 's/.* \([^ ]*\) *$/\1/'` 296 | file=`sed -n '/^@setfilename/ { s/.* \([^ ]*\) *$/\1/; p; q; }' $infile` 297 | # ... or it is derived from the source name (dir/f.texi becomes f.info) 298 | test -z "$file" && file=`echo "$infile" | sed 's,.*/,,;s,.[^.]*$,,'`.info 299 | fi 300 | # If the file does not exist, the user really needs makeinfo; 301 | # let's fail without touching anything. 302 | test -f $file || exit 1 303 | touch $file 304 | ;; 305 | 306 | tar) 307 | shift 308 | 309 | # We have already tried tar in the generic part. 310 | # Look for gnutar/gtar before invocation to avoid ugly error 311 | # messages. 312 | if (gnutar --version > /dev/null 2>&1); then 313 | gnutar "$@" && exit 0 314 | fi 315 | if (gtar --version > /dev/null 2>&1); then 316 | gtar "$@" && exit 0 317 | fi 318 | firstarg="$1" 319 | if shift; then 320 | case "$firstarg" in 321 | *o*) 322 | firstarg=`echo "$firstarg" | sed s/o//` 323 | tar "$firstarg" "$@" && exit 0 324 | ;; 325 | esac 326 | case "$firstarg" in 327 | *h*) 328 | firstarg=`echo "$firstarg" | sed s/h//` 329 | tar "$firstarg" "$@" && exit 0 330 | ;; 331 | esac 332 | fi 333 | 334 | echo 1>&2 "\ 335 | WARNING: I can't seem to be able to run \`tar' with the given arguments. 336 | You may want to install GNU tar or Free paxutils, or check the 337 | command line arguments." 338 | exit 1 339 | ;; 340 | 341 | *) 342 | echo 1>&2 "\ 343 | WARNING: \`$1' is needed, and is $msg. 344 | You might have modified some files without having the 345 | proper tools for further handling them. Check the \`README' file, 346 | it often tells you about the needed prerequisites for installing 347 | this package. You may also peek at any GNU archive site, in case 348 | some other package would contain this missing \`$1' program." 349 | exit 1 350 | ;; 351 | esac 352 | 353 | exit 0 354 | 355 | # Local variables: 356 | # eval: (add-hook 'write-file-hooks 'time-stamp) 357 | # time-stamp-start: "scriptversion=" 358 | # time-stamp-format: "%:y-%02m-%02d.%02H" 359 | # time-stamp-end: "$" 360 | # End: 361 | -------------------------------------------------------------------------------- /src/BootControlBlocks.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2014 Freescale Semiconductor, Inc. All Rights Reserved. 3 | */ 4 | 5 | /* 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along 17 | * with this program; if not, write to the Free Software Foundation, Inc., 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 19 | */ 20 | #ifndef BOOTCONTROLBLOCKS_H_ 21 | #define BOOTCONTROLBLOCKS_H_ 22 | 23 | #define NCB_FINGERPRINT1 0x504d5453 //!< 'STMP' 24 | #define NCB_FINGERPRINT2 0x2042434e //!< 'NCB' - NAND Control Block 25 | #define NCB_FINGERPRINT3 0x4e494252 //!< 'RBIN' - ROM Boot Image Block - N 26 | 27 | #define FCB_FINGERPRINT 0x20424346 //!< 'FCB ' 28 | #define FCB_VERSION_1 0x01000000 29 | 30 | #define LDLB_FINGERPRINT1 0x504d5453 //!< 'STMP' 31 | #define LDLB_FINGERPRINT2 0x424c444c //!< 'LDLB' - Logical Device Layout Block 32 | #define LDLB_FINGERPRINT3 0x4c494252 //!< 'RBIL' - ROM Boot Image Block - L 33 | 34 | #define DBBT_FINGERPRINT1 0x504d5453 //!< 'STMP' 35 | #define DBBT_FINGERPRINT2_V2 0x44424254 //!< 'DBBT' - Discovered Bad Block Table. 36 | #define DBBT_FINGERPRINT2 0x54424244 //!< 'DBBT' - Discovered Bad Block Table. 37 | #define DBBT_FINGERPRINT3 0x44494252 //!< 'RBID' - ROM Boot Image Block - D 38 | #define DBBT_VERSION_1 0x01000000 39 | 40 | #define LDLB_VERSION_MAJOR 0x0001 41 | #define LDLB_VERSION_MINOR 0x0000 42 | #define LDLB_VERSION_SUB 0x0000 43 | 44 | #define TYPICAL_NAND_READ_SIZE 2048 45 | 46 | #define BCB_MAGIC_OFFSET 12 47 | 48 | #define MAXSEQLEN 183 49 | 50 | //============================================================================== 51 | 52 | //! \brief NAND Timing structure for setting up the GPMI timing. 53 | //! 54 | //! This structure holds the timing for the NAND. This data is used by 55 | //! rom_nand_hal_GpmiSetNandTiming to setup the GPMI hardware registers. 56 | 57 | #include 58 | 59 | typedef struct _NAND_Timing { 60 | uint8_t m_u8DataSetup; 61 | uint8_t m_u8DataHold; 62 | uint8_t m_u8AddressSetup; 63 | uint8_t m_u8DSAMPLE_TIME; 64 | } NCB_NAND_Timing_t; 65 | 66 | //============================================================================== 67 | 68 | //! \brief Structure defining where NCB and LDLB parameters are located. 69 | //! 70 | //! This structure defines the basic fingerprint template for both the Nand 71 | //! Control Block (NCB) and the Logical Drive Layout Block (LDLB). This 72 | //! template is used to determine if the sector read is a Boot Control Block. 73 | //! This structure defines the NAND Control Block (NCB). This block 74 | //! contains information describing the timing for the NAND, the number of 75 | //! NANDs in the system, the block size of the NAND, the page size of the NAND, 76 | //! and other criteria for the NAND. This is information that is 77 | //! required just to successfully communicate with the NAND. 78 | //! This structure also defines the Logical Drive Layout Block (LDLB). This 79 | //! block contains information describing the version as well as the layout of 80 | //! the code and data on the NAND Media. For the ROM, we're only concerned 81 | //! with the boot firmware start. Additional information may be stored in 82 | //! the Reserved3 area. This area will be of interest to the SDK. 83 | //! This structure also defines the Discovered Bad Block Table (DBBT) header. 84 | //! This block contains the information used for parsing the bad block tables 85 | //! which are stored in subsequent 2K sectors. The DBBT header is 8K, followed 86 | //! by the first NANDs entries, then the 2nd NANDs entries on a subsequent 2K 87 | //! page (determined by how many 2K pages the first nand requires), and so on. 88 | 89 | typedef struct _NCB_BootBlockStruct_t { 90 | uint32_t m_u32FingerPrint1; //!< First fingerprint in first byte. 91 | union { 92 | struct { 93 | NCB_NAND_Timing_t m_NANDTiming; //!< Optimum timing parameters for Tas, Tds, Tdh in nsec. 94 | uint32_t m_u32DataPageSize; //!< 2048 for 2K pages, 4096 for 4K pages. 95 | uint32_t m_u32TotalPageSize; //!< 2112 for 2K pages, 4314 for 4K pages. 96 | uint32_t m_u32SectorsPerBlock; //!< Number of 2K sections per block. 97 | uint32_t m_u32SectorInPageMask; //!< Mask for handling pages > 2K. 98 | uint32_t m_u32SectorToPageShift; //!< Address shift for handling pages > 2K. 99 | uint32_t m_u32NumberOfNANDs; //!< Total Number of NANDs - not used by ROM. 100 | } NCB_Block1; 101 | struct { 102 | struct { 103 | uint16_t m_u16Major; 104 | uint16_t m_u16Minor; 105 | uint16_t m_u16Sub; 106 | uint16_t m_u16Reserved; 107 | } LDLB_Version; //!< LDLB version - not used by ROM. 108 | uint32_t m_u32NANDBitmap; //!< bit 0 == NAND 0, bit 1 == NAND 1, bit 2 = NAND 2, bit 3 = NAND3 109 | } LDLB_Block1; 110 | struct { 111 | uint32_t m_u32NumberBB_NAND0; //!< # Bad Blocks stored in this table for NAND0. 112 | uint32_t m_u32NumberBB_NAND1; //!< # Bad Blocks stored in this table for NAND1. 113 | uint32_t m_u32NumberBB_NAND2; //!< # Bad Blocks stored in this table for NAND2. 114 | uint32_t m_u32NumberBB_NAND3; //!< # Bad Blocks stored in this table for NAND3. 115 | uint32_t m_u32Number2KPagesBB_NAND0; //!< Bad Blocks for NAND0 consume this # of 2K pages. 116 | uint32_t m_u32Number2KPagesBB_NAND1; //!< Bad Blocks for NAND1 consume this # of 2K pages. 117 | uint32_t m_u32Number2KPagesBB_NAND2; //!< Bad Blocks for NAND2 consume this # of 2K pages. 118 | uint32_t m_u32Number2KPagesBB_NAND3; //!< Bad Blocks for NAND3 consume this # of 2K pages. 119 | } DBBT_Block1; 120 | // This one just forces the spacing. 121 | uint32_t m_Reserved1[10]; 122 | }; 123 | uint32_t m_u32FingerPrint2; //!< 2nd fingerprint at byte 10. 124 | union { 125 | struct { 126 | uint32_t m_u32NumRowBytes; //!< Number of row bytes in read/write transactions. 127 | uint32_t m_u32NumColumnBytes; //!< Number of row bytes in read/write transactions. 128 | uint32_t m_u32TotalInternalDie; //!< Number of separate chips in this NAND. 129 | uint32_t m_u32InternalPlanesPerDie; //!< Number of internal planes - treat like separate chips. 130 | uint32_t m_u32CellType; //!< MLC or SLC. 131 | uint32_t m_u32ECCType; //!< 4 symbol or 8 symbol ECC? 132 | 133 | /**********************************/ 134 | uint32_t m_u32EccBlock0Size; //!< Number of bytes for Block0 - BCH 135 | uint32_t m_u32EccBlockNSize; //!< Block size in bytes for all blocks other than Block0 - BCH 136 | uint32_t m_u32EccBlock0EccLevel; //!< Ecc level for Block 0 - BCH 137 | uint32_t m_u32NumEccBlocksPerPage; //!< Number of blocks per page - BCH 138 | uint32_t m_u32MetadataBytes; //!< Metadata size - BCH 139 | uint32_t m_u32EraseThreshold; //!< To set into BCH_MODE register. 140 | /**************** above is NCBv2 */ 141 | 142 | uint32_t m_u32Read1stCode; //!< First value sent to initiate a NAND Read sequence. 143 | uint32_t m_u32Read2ndCode; //!< Second value sent to initiate a NAND Read sequence. 144 | uint32_t m_u32BootPatch; 145 | uint32_t m_u32PatchSectors; 146 | uint32_t m_u32Firmware_startingNAND2; 147 | } NCB_Block2; 148 | struct { 149 | uint32_t m_u32Firmware_startingNAND; //!< Firmware image starts on this NAND. 150 | uint32_t m_u32Firmware_startingSector; //!< Firmware image starts on this sector. 151 | uint32_t m_u32Firmware_sectorStride; //!< Amount to jump between sectors - unused in ROM. 152 | uint32_t m_uSectorsInFirmware; //!< Number of sectors in firmware image. 153 | uint32_t m_u32Firmware_startingNAND2; //!< Secondary FW Image starting NAND. 154 | uint32_t m_u32Firmware_startingSector2; //!< Secondary FW Image starting Sector. 155 | uint32_t m_u32Firmware_sectorStride2; //!< Secondary FW Image stride - unused in ROM. 156 | uint32_t m_uSectorsInFirmware2; //!< Number of sector in secondary FW image. 157 | struct { 158 | uint16_t m_u16Major; 159 | uint16_t m_u16Minor; 160 | uint16_t m_u16Sub; 161 | uint16_t m_u16Reserved; 162 | } FirmwareVersion; 163 | uint32_t m_u32DiscoveredBBTableSector; //!< Location of Discovered Bad Block Table (DBBT). 164 | uint32_t m_u32DiscoveredBBTableSector2; //!< Location of backup DBBT 165 | } LDLB_Block2; 166 | // This one just forces the spacing. 167 | uint32_t m_Reserved2[20]; 168 | }; 169 | uint32_t m_u32FingerPrint3; //!< 3rd fingerprint at byte 30. 170 | union { 171 | struct { 172 | unsigned char ncb_unknown[12]; 173 | }; 174 | }; 175 | } NCB_BootBlockStruct_t; 176 | 177 | typedef enum _nand_ecc_type { 178 | RS_Ecc_4bit = 0, 179 | RS_Ecc_8bit, 180 | BCH_Ecc_0bit, 181 | BCH_Ecc_2bit, 182 | BCH_Ecc_4bit, 183 | BCH_Ecc_6bit, 184 | BCH_Ecc_8bit, 185 | BCH_Ecc_10bit, 186 | BCH_Ecc_12bit, 187 | BCH_Ecc_14bit, 188 | BCH_Ecc_16bit, 189 | BCH_Ecc_18bit, 190 | BCH_Ecc_20bit 191 | } nand_ecc_type_t; 192 | 193 | typedef enum { 194 | ROM_BCH_Ecc_0bit = 0, 195 | ROM_BCH_Ecc_2bit, 196 | ROM_BCH_Ecc_4bit, 197 | ROM_BCH_Ecc_6bit, 198 | ROM_BCH_Ecc_8bit, 199 | ROM_BCH_Ecc_10bit, 200 | ROM_BCH_Ecc_12bit, 201 | ROM_BCH_Ecc_14bit, 202 | ROM_BCH_Ecc_16bit, 203 | ROM_BCH_Ecc_18bit, 204 | ROM_BCH_Ecc_20bit 205 | } rom_ecc_type_t; 206 | 207 | //============================================================================== 208 | 209 | //! \brief Structure of the Bad Block Entry Table in NAND. 210 | //! 211 | //! This structure defines the Discovered Bad Block Table (DBBT) entries. This 212 | //! block contains a word holding the NAND number then a word describing the number 213 | //! of Bad Blocks on the NAND and an array containing these bad blocks. The ROM 214 | //! will use these entries in the Bad Block table to correctly index to the next 215 | //! sector (skip over bad blocks) while reading from the NAND. 216 | //! Blocks are not guaranteed to be sorted in this table. 217 | typedef struct _BadBlockTableNand_t { 218 | uint32_t uNAND; //!< Which NAND this table is for. 219 | uint32_t uNumberBB; //!< Number of Bad Blocks in this NAND. 220 | // Divide by 4 because of 32 bit words. Subtract 2 because of the 2 above 221 | // 32 bit words. 222 | uint32_t u32BadBlock[(TYPICAL_NAND_READ_SIZE / 4) - 2]; //!< Table of the Bad Blocks. 223 | } BadBlockTableNand_t; 224 | 225 | //============================================================================== 226 | 227 | //! \brief NAND Timing structure for setting up the GPMI timing. 228 | //! 229 | //! This structure holds the timing for the NAND. This data is used by 230 | //! rom_nand_hal_GpmiSetNandTiming to setup the GPMI hardware registers. 231 | typedef struct { 232 | uint8_t m_u8DataSetup; 233 | uint8_t m_u8DataHold; 234 | uint8_t m_u8AddressSetup; 235 | uint8_t m_u8DSAMPLE_TIME; 236 | /* These are for application use only and not for ROM. */ 237 | uint8_t m_u8NandTimingState; 238 | uint8_t m_u8REA; 239 | uint8_t m_u8RLOH; 240 | uint8_t m_u8RHOH; 241 | } FCB_ROM_NAND_Timing_t; 242 | 243 | typedef struct { 244 | uint32_t m_u32TMTiming2_ReadLatency; 245 | uint32_t m_u32TMTiming2_PreambleDelay; 246 | uint32_t m_u32TMTiming2_CEDelay; 247 | uint32_t m_u32TMTiming2_PostambleDelay; 248 | uint32_t m_u32TMTiming2_CmdAddPause; 249 | uint32_t m_u32TMTiming2_DataPause; 250 | uint32_t m_u32TMSpeed; 251 | uint32_t m_u32TMTiming1_BusyTimeout; 252 | } FCB_ROM_NAND_TM_Timing_t; 253 | 254 | typedef struct { 255 | uint32_t m_u32ONFISpeed; 256 | uint32_t m_u32ONFITiming_ReadLatency; 257 | uint32_t m_u32ONFITiming_CEDelay; 258 | uint32_t m_u32ONFITiming_PreambleDelay; 259 | uint32_t m_u32ONFITiming_PostambleDelay; 260 | uint32_t m_u32ONFITiming_CmdAddPause; 261 | uint32_t m_u32ONFITiming_DataPause; 262 | uint32_t m_u32ONFITiming_BusyTimeout; 263 | } FCB_ROM_NAND_ONFI_Timing_t; 264 | 265 | struct fcb_block { 266 | FCB_ROM_NAND_Timing_t m_NANDTiming; //!< Optimum timing parameters for Tas, Tds, Tdh in nsec. 267 | uint32_t m_u32PageDataSize; //!< 2048 for 2K pages, 4096 for 4K pages. 268 | uint32_t m_u32TotalPageSize; //!< 2112 for 2K pages, 4314 for 4K pages. 269 | uint32_t m_u32SectorsPerBlock; //!< Number of 2K sections per block. 270 | uint32_t m_u32NumberOfNANDs; //!< Total Number of NANDs - not used by ROM. 271 | uint32_t m_u32TotalInternalDie; //!< Number of separate chips in this NAND. 272 | uint32_t m_u32CellType; //!< MLC or SLC. 273 | uint32_t m_u32EccBlockNEccType; //!< Type of ECC, can be one of BCH-0-20 274 | uint32_t m_u32EccBlock0Size; //!< Number of bytes for Block0 - BCH 275 | uint32_t m_u32EccBlockNSize; //!< Block size in bytes for all blocks other than Block0 - BCH 276 | uint32_t m_u32EccBlock0EccType; //!< Ecc level for Block 0 - BCH 277 | uint32_t m_u32MetadataBytes; //!< Metadata size - BCH 278 | uint32_t m_u32NumEccBlocksPerPage; //!< Number of blocks per page for ROM use - BCH 279 | uint32_t m_u32EccBlockNEccLevelSDK; //!< Type of ECC, can be one of BCH-0-20 280 | uint32_t m_u32EccBlock0SizeSDK; //!< Number of bytes for Block0 - BCH 281 | uint32_t m_u32EccBlockNSizeSDK; //!< Block size in bytes for all blocks other than Block0 - BCH 282 | uint32_t m_u32EccBlock0EccLevelSDK; //!< Ecc level for Block 0 - BCH 283 | uint32_t m_u32NumEccBlocksPerPageSDK; //!< Number of blocks per page for SDK use - BCH 284 | uint32_t m_u32MetadataBytesSDK; //!< Metadata size - BCH 285 | uint32_t m_u32EraseThreshold; //!< To set into BCH_MODE register. 286 | uint32_t m_u32BootPatch; //!< 0 for normal boot and 1 to load patch starting next to FCB. 287 | uint32_t m_u32PatchSectors; //!< Size of patch in sectors. 288 | uint32_t m_u32Firmware1_startingPage; //!< Firmware image starts on this sector. 289 | uint32_t m_u32Firmware2_startingPage; //!< Secondary FW Image starting Sector. 290 | uint32_t m_u32PagesInFirmware1; //!< Number of sectors in firmware image. 291 | uint32_t m_u32PagesInFirmware2; //!< Number of sector in secondary FW image. 292 | uint32_t m_u32DBBTSearchAreaStartAddress;//!< Page address where dbbt search area begins 293 | uint32_t m_u32BadBlockMarkerByte; //!< Byte in page data that have manufacturer marked bad block marker, this will 294 | //!< bw swapped with metadata[0] to complete page data. 295 | uint32_t m_u32BadBlockMarkerStartBit; //!< For BCH ECC sizes other than 8 and 16 the bad block marker does not start 296 | //!< at 0th bit of m_u32BadBlockMarkerByte. This field is used to get to the 297 | //!< start bit of bad block marker byte with in m_u32BadBlockMarkerByte. 298 | uint32_t m_u32BBMarkerPhysicalOffset; //!< FCB value that gives byte offset for bad block marker on physical NAND page. 299 | uint32_t m_u32BCHType; 300 | FCB_ROM_NAND_TM_Timing_t m_NANDTMTiming; 301 | uint32_t m_u32DISBBM; /* the flag to enable (1)/disable(0) bi swap */ 302 | uint32_t m_u32BBMarkerPhysicalOffsetInSpareData; /* The swap position of main area in spare area */ 303 | 304 | uint32_t m_u32OnfiSyncEnable; //!< Enable the Onfi nand sync mode support 305 | FCB_ROM_NAND_ONFI_Timing_t m_NANDONFITiming; 306 | uint32_t m_u32DISBBSearch; //!< Disable the badblock search when reading the firmware, only using DBBT. 307 | 308 | uint32_t m_u32RandomizerEnable; //!< Enable randomizer support 309 | uint32_t reserved[15]; 310 | uint32_t m_u32ReadRetryEnable; //!< Enable ready retry support 311 | uint32_t m_u32ReadRetrySeqLength; //!< Read retry sequence length 312 | uint32_t m_u32ReadRetrySeq[MAXSEQLEN]; //!< Read retry sequence length 313 | }; 314 | 315 | //============================================================================== 316 | 317 | //! \brief Structure defining where FCB and DBBT parameters are located. 318 | //! 319 | //! This structure defines the basic fingerprint template for both the Firmware 320 | //! Control Block (FCB) and the Discovered Bad Block Table (DBBT). This 321 | //! template is used to determine if the sector read is a Boot Control Block. 322 | //! This structure defines the Firmware Control Block (FCB). This block 323 | //! contains information describing the timing for the NAND, the number of 324 | //! NANDs in the system, the block size of the NAND, the page size of the NAND, 325 | //! and other criteria for the NAND. This is information that is 326 | //! required just to successfully communicate with the NAND. 327 | //! This block contains information describing the version as well as the layout of 328 | //! the code and data on the NAND Media. For the ROM, we're only concerned 329 | //! with the boot firmware start. Additional information may be stored in 330 | //! the Reserved area. This area will be of interest to the SDK. 331 | //! This structure also defines the Discovered Bad Block Table (DBBT) header. 332 | //! This block contains the information used for parsing the bad block tables 333 | //! which are stored in subsequent 2K sectors. The DBBT header is 8K, followed 334 | //! by the first NANDs entries on a subsequent 2K page (determined by how many 335 | //! 2K pages the first nand requires) 336 | typedef struct { 337 | uint32_t m_u32Checksum; //!< First fingerprint in first byte. 338 | uint32_t m_u32FingerPrint; //!< 2nd fingerprint at byte 4. 339 | uint32_t m_u32Version; //!< 3rd fingerprint at byte 8. 340 | union { 341 | struct fcb_block FCB_Block; 342 | union { 343 | struct { 344 | uint32_t m_u32NumberBB; //!< # Bad Blocks stored in this table for NAND0. 345 | uint32_t m_u32Number2KPagesBB; //!< Bad Blocks for NAND0 consume this # of 2K pages. 346 | } v2; 347 | struct { 348 | uint32_t m_u32res; 349 | uint32_t m_u32DBBTNumOfPages; 350 | } v3; 351 | } DBBT_Block; 352 | }; 353 | } BCB_ROM_BootBlockStruct_t; 354 | 355 | #endif /*BOOTCONTROLBLOCKS_H_ */ 356 | 357 | -------------------------------------------------------------------------------- /src/Makefile.am: -------------------------------------------------------------------------------- 1 | INCLUDES = -I$(top_srcdir)/include 2 | 3 | bin_PROGRAMS=kobs-ng 4 | 5 | kobs_ng_SOURCES=main.c mtd.c rom_nand_hamming_code_ecc.c ncb.c bootstream.c sha1.c \ 6 | aescrypt.c aeskey.c aestab.c plat_boot_config.c bch.c \ 7 | rom_nand_hamming_code_ecc.h mtd.h BootControlBlocks.h bootstream.h sha.h \ 8 | aes.h aesopt.h plat_boot_config.h bch.h rand.h 9 | -------------------------------------------------------------------------------- /src/Makefile.in: -------------------------------------------------------------------------------- 1 | # Makefile.in generated by automake 1.11.1 from Makefile.am. 2 | # @configure_input@ 3 | 4 | # Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 5 | # 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, 6 | # Inc. 7 | # This Makefile.in is free software; the Free Software Foundation 8 | # gives unlimited permission to copy and/or distribute it, 9 | # with or without modifications, as long as this notice is preserved. 10 | 11 | # This program is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY, to the extent permitted by law; without 13 | # even the implied warranty of MERCHANTABILITY or FITNESS FOR A 14 | # PARTICULAR PURPOSE. 15 | 16 | @SET_MAKE@ 17 | 18 | VPATH = @srcdir@ 19 | pkgdatadir = $(datadir)/@PACKAGE@ 20 | pkgincludedir = $(includedir)/@PACKAGE@ 21 | pkglibdir = $(libdir)/@PACKAGE@ 22 | pkglibexecdir = $(libexecdir)/@PACKAGE@ 23 | am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd 24 | install_sh_DATA = $(install_sh) -c -m 644 25 | install_sh_PROGRAM = $(install_sh) -c 26 | install_sh_SCRIPT = $(install_sh) -c 27 | INSTALL_HEADER = $(INSTALL_DATA) 28 | transform = $(program_transform_name) 29 | NORMAL_INSTALL = : 30 | PRE_INSTALL = : 31 | POST_INSTALL = : 32 | NORMAL_UNINSTALL = : 33 | PRE_UNINSTALL = : 34 | POST_UNINSTALL = : 35 | bin_PROGRAMS = kobs-ng$(EXEEXT) 36 | subdir = src 37 | DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in 38 | ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 39 | am__aclocal_m4_deps = $(top_srcdir)/acinclude.m4 \ 40 | $(top_srcdir)/configure.in 41 | am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ 42 | $(ACLOCAL_M4) 43 | mkinstalldirs = $(install_sh) -d 44 | CONFIG_HEADER = $(top_builddir)/include/config.h 45 | CONFIG_CLEAN_FILES = 46 | CONFIG_CLEAN_VPATH_FILES = 47 | am__installdirs = "$(DESTDIR)$(bindir)" 48 | PROGRAMS = $(bin_PROGRAMS) 49 | am_kobs_ng_OBJECTS = main.$(OBJEXT) mtd.$(OBJEXT) \ 50 | rom_nand_hamming_code_ecc.$(OBJEXT) ncb.$(OBJEXT) \ 51 | bootstream.$(OBJEXT) sha1.$(OBJEXT) aescrypt.$(OBJEXT) \ 52 | aeskey.$(OBJEXT) aestab.$(OBJEXT) plat_boot_config.$(OBJEXT) \ 53 | bch.$(OBJEXT) 54 | kobs_ng_OBJECTS = $(am_kobs_ng_OBJECTS) 55 | kobs_ng_LDADD = $(LDADD) 56 | DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/include 57 | depcomp = $(SHELL) $(top_srcdir)/depcomp 58 | am__depfiles_maybe = depfiles 59 | am__mv = mv -f 60 | COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ 61 | $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) 62 | CCLD = $(CC) 63 | LINK = $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@ 64 | SOURCES = $(kobs_ng_SOURCES) 65 | DIST_SOURCES = $(kobs_ng_SOURCES) 66 | ETAGS = etags 67 | CTAGS = ctags 68 | DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) 69 | ACLOCAL = @ACLOCAL@ 70 | AMTAR = @AMTAR@ 71 | AUTOCONF = @AUTOCONF@ 72 | AUTOHEADER = @AUTOHEADER@ 73 | AUTOMAKE = @AUTOMAKE@ 74 | AWK = @AWK@ 75 | CC = @CC@ 76 | CCDEPMODE = @CCDEPMODE@ 77 | CFLAGS = @CFLAGS@ 78 | CPP = @CPP@ 79 | CPPFLAGS = @CPPFLAGS@ 80 | CYGPATH_W = @CYGPATH_W@ 81 | DEFS = @DEFS@ 82 | DEPDIR = @DEPDIR@ 83 | ECHO_C = @ECHO_C@ 84 | ECHO_N = @ECHO_N@ 85 | ECHO_T = @ECHO_T@ 86 | EGREP = @EGREP@ 87 | EXEEXT = @EXEEXT@ 88 | GREP = @GREP@ 89 | INSTALL = @INSTALL@ 90 | INSTALL_DATA = @INSTALL_DATA@ 91 | INSTALL_PROGRAM = @INSTALL_PROGRAM@ 92 | INSTALL_SCRIPT = @INSTALL_SCRIPT@ 93 | INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ 94 | KOBS_NG_MAJOR = @KOBS_NG_MAJOR@ 95 | KOBS_NG_MINOR = @KOBS_NG_MINOR@ 96 | KOBS_NG_SUBMINOR = @KOBS_NG_SUBMINOR@ 97 | KOBS_NG_VERSION = @KOBS_NG_VERSION@ 98 | LDFLAGS = @LDFLAGS@ 99 | LIBOBJS = @LIBOBJS@ 100 | LIBS = @LIBS@ 101 | LN_S = @LN_S@ 102 | LTLIBOBJS = @LTLIBOBJS@ 103 | MAKEINFO = @MAKEINFO@ 104 | MKDIR_P = @MKDIR_P@ 105 | OBJEXT = @OBJEXT@ 106 | PACKAGE = @PACKAGE@ 107 | PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@ 108 | PACKAGE_NAME = @PACKAGE_NAME@ 109 | PACKAGE_STRING = @PACKAGE_STRING@ 110 | PACKAGE_TARNAME = @PACKAGE_TARNAME@ 111 | PACKAGE_URL = @PACKAGE_URL@ 112 | PACKAGE_VERSION = @PACKAGE_VERSION@ 113 | PATH_SEPARATOR = @PATH_SEPARATOR@ 114 | SET_MAKE = @SET_MAKE@ 115 | SHELL = @SHELL@ 116 | STRIP = @STRIP@ 117 | VERSION = @VERSION@ 118 | abs_builddir = @abs_builddir@ 119 | abs_srcdir = @abs_srcdir@ 120 | abs_top_builddir = @abs_top_builddir@ 121 | abs_top_srcdir = @abs_top_srcdir@ 122 | ac_ct_CC = @ac_ct_CC@ 123 | am__include = @am__include@ 124 | am__leading_dot = @am__leading_dot@ 125 | am__quote = @am__quote@ 126 | am__tar = @am__tar@ 127 | am__untar = @am__untar@ 128 | bindir = @bindir@ 129 | build_alias = @build_alias@ 130 | builddir = @builddir@ 131 | datadir = @datadir@ 132 | datarootdir = @datarootdir@ 133 | docdir = @docdir@ 134 | dvidir = @dvidir@ 135 | exec_prefix = @exec_prefix@ 136 | host_alias = @host_alias@ 137 | htmldir = @htmldir@ 138 | includedir = @includedir@ 139 | infodir = @infodir@ 140 | install_sh = @install_sh@ 141 | libdir = @libdir@ 142 | libexecdir = @libexecdir@ 143 | localedir = @localedir@ 144 | localstatedir = @localstatedir@ 145 | mandir = @mandir@ 146 | mkdir_p = @mkdir_p@ 147 | oldincludedir = @oldincludedir@ 148 | pdfdir = @pdfdir@ 149 | prefix = @prefix@ 150 | program_transform_name = @program_transform_name@ 151 | psdir = @psdir@ 152 | sbindir = @sbindir@ 153 | sharedstatedir = @sharedstatedir@ 154 | srcdir = @srcdir@ 155 | sysconfdir = @sysconfdir@ 156 | target_alias = @target_alias@ 157 | top_build_prefix = @top_build_prefix@ 158 | top_builddir = @top_builddir@ 159 | top_srcdir = @top_srcdir@ 160 | INCLUDES = -I$(top_srcdir)/include 161 | kobs_ng_SOURCES = main.c mtd.c rom_nand_hamming_code_ecc.c ncb.c bootstream.c sha1.c \ 162 | aescrypt.c aeskey.c aestab.c plat_boot_config.c bch.c\ 163 | rom_nand_hamming_code_ecc.h mtd.h BootControlBlocks.h bootstream.h sha.h \ 164 | aes.h aesopt.h plat_boot_config.h bch.h rand.h 165 | 166 | all: all-am 167 | 168 | .SUFFIXES: 169 | .SUFFIXES: .c .o .obj 170 | $(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps) 171 | @for dep in $?; do \ 172 | case '$(am__configure_deps)' in \ 173 | *$$dep*) \ 174 | ( cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh ) \ 175 | && { if test -f $@; then exit 0; else break; fi; }; \ 176 | exit 1;; \ 177 | esac; \ 178 | done; \ 179 | echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu src/Makefile'; \ 180 | $(am__cd) $(top_srcdir) && \ 181 | $(AUTOMAKE) --gnu src/Makefile 182 | .PRECIOUS: Makefile 183 | Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status 184 | @case '$?' in \ 185 | *config.status*) \ 186 | cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \ 187 | *) \ 188 | echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)'; \ 189 | cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe);; \ 190 | esac; 191 | 192 | $(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) 193 | cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh 194 | 195 | $(top_srcdir)/configure: $(am__configure_deps) 196 | cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh 197 | $(ACLOCAL_M4): $(am__aclocal_m4_deps) 198 | cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh 199 | $(am__aclocal_m4_deps): 200 | install-binPROGRAMS: $(bin_PROGRAMS) 201 | @$(NORMAL_INSTALL) 202 | test -z "$(bindir)" || $(MKDIR_P) "$(DESTDIR)$(bindir)" 203 | @list='$(bin_PROGRAMS)'; test -n "$(bindir)" || list=; \ 204 | for p in $$list; do echo "$$p $$p"; done | \ 205 | sed 's/$(EXEEXT)$$//' | \ 206 | while read p p1; do if test -f $$p; \ 207 | then echo "$$p"; echo "$$p"; else :; fi; \ 208 | done | \ 209 | sed -e 'p;s,.*/,,;n;h' -e 's|.*|.|' \ 210 | -e 'p;x;s,.*/,,;s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/' | \ 211 | sed 'N;N;N;s,\n, ,g' | \ 212 | $(AWK) 'BEGIN { files["."] = ""; dirs["."] = 1 } \ 213 | { d=$$3; if (dirs[d] != 1) { print "d", d; dirs[d] = 1 } \ 214 | if ($$2 == $$4) files[d] = files[d] " " $$1; \ 215 | else { print "f", $$3 "/" $$4, $$1; } } \ 216 | END { for (d in files) print "f", d, files[d] }' | \ 217 | while read type dir files; do \ 218 | if test "$$dir" = .; then dir=; else dir=/$$dir; fi; \ 219 | test -z "$$files" || { \ 220 | echo " $(INSTALL_PROGRAM_ENV) $(INSTALL_PROGRAM) $$files '$(DESTDIR)$(bindir)$$dir'"; \ 221 | $(INSTALL_PROGRAM_ENV) $(INSTALL_PROGRAM) $$files "$(DESTDIR)$(bindir)$$dir" || exit $$?; \ 222 | } \ 223 | ; done 224 | 225 | uninstall-binPROGRAMS: 226 | @$(NORMAL_UNINSTALL) 227 | @list='$(bin_PROGRAMS)'; test -n "$(bindir)" || list=; \ 228 | files=`for p in $$list; do echo "$$p"; done | \ 229 | sed -e 'h;s,^.*/,,;s/$(EXEEXT)$$//;$(transform)' \ 230 | -e 's/$$/$(EXEEXT)/' `; \ 231 | test -n "$$list" || exit 0; \ 232 | echo " ( cd '$(DESTDIR)$(bindir)' && rm -f" $$files ")"; \ 233 | cd "$(DESTDIR)$(bindir)" && rm -f $$files 234 | 235 | clean-binPROGRAMS: 236 | -test -z "$(bin_PROGRAMS)" || rm -f $(bin_PROGRAMS) 237 | kobs-ng$(EXEEXT): $(kobs_ng_OBJECTS) $(kobs_ng_DEPENDENCIES) 238 | @rm -f kobs-ng$(EXEEXT) 239 | $(LINK) $(kobs_ng_OBJECTS) $(kobs_ng_LDADD) $(LIBS) 240 | 241 | mostlyclean-compile: 242 | -rm -f *.$(OBJEXT) 243 | 244 | distclean-compile: 245 | -rm -f *.tab.c 246 | 247 | @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/aescrypt.Po@am__quote@ 248 | @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/aeskey.Po@am__quote@ 249 | @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/aestab.Po@am__quote@ 250 | @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/bootstream.Po@am__quote@ 251 | @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main.Po@am__quote@ 252 | @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mtd.Po@am__quote@ 253 | @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ncb.Po@am__quote@ 254 | @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/plat_boot_config.Po@am__quote@ 255 | @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rom_nand_hamming_code_ecc.Po@am__quote@ 256 | @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sha1.Po@am__quote@ 257 | @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/bch.Po@am__quote@ 258 | 259 | .c.o: 260 | @am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< 261 | @am__fastdepCC_TRUE@ $(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po 262 | @AMDEP_TRUE@@am__fastdepCC_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ 263 | @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ 264 | @am__fastdepCC_FALSE@ $(COMPILE) -c $< 265 | 266 | .c.obj: 267 | @am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'` 268 | @am__fastdepCC_TRUE@ $(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po 269 | @AMDEP_TRUE@@am__fastdepCC_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ 270 | @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ 271 | @am__fastdepCC_FALSE@ $(COMPILE) -c `$(CYGPATH_W) '$<'` 272 | 273 | ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES) 274 | list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ 275 | unique=`for i in $$list; do \ 276 | if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ 277 | done | \ 278 | $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ 279 | END { if (nonempty) { for (i in files) print i; }; }'`; \ 280 | mkid -fID $$unique 281 | tags: TAGS 282 | 283 | TAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \ 284 | $(TAGS_FILES) $(LISP) 285 | set x; \ 286 | here=`pwd`; \ 287 | list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ 288 | unique=`for i in $$list; do \ 289 | if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ 290 | done | \ 291 | $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ 292 | END { if (nonempty) { for (i in files) print i; }; }'`; \ 293 | shift; \ 294 | if test -z "$(ETAGS_ARGS)$$*$$unique"; then :; else \ 295 | test -n "$$unique" || unique=$$empty_fix; \ 296 | if test $$# -gt 0; then \ 297 | $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ 298 | "$$@" $$unique; \ 299 | else \ 300 | $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ 301 | $$unique; \ 302 | fi; \ 303 | fi 304 | ctags: CTAGS 305 | CTAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \ 306 | $(TAGS_FILES) $(LISP) 307 | list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ 308 | unique=`for i in $$list; do \ 309 | if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ 310 | done | \ 311 | $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ 312 | END { if (nonempty) { for (i in files) print i; }; }'`; \ 313 | test -z "$(CTAGS_ARGS)$$unique" \ 314 | || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \ 315 | $$unique 316 | 317 | GTAGS: 318 | here=`$(am__cd) $(top_builddir) && pwd` \ 319 | && $(am__cd) $(top_srcdir) \ 320 | && gtags -i $(GTAGS_ARGS) "$$here" 321 | 322 | distclean-tags: 323 | -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags 324 | 325 | distdir: $(DISTFILES) 326 | @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ 327 | topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ 328 | list='$(DISTFILES)'; \ 329 | dist_files=`for file in $$list; do echo $$file; done | \ 330 | sed -e "s|^$$srcdirstrip/||;t" \ 331 | -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \ 332 | case $$dist_files in \ 333 | */*) $(MKDIR_P) `echo "$$dist_files" | \ 334 | sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \ 335 | sort -u` ;; \ 336 | esac; \ 337 | for file in $$dist_files; do \ 338 | if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ 339 | if test -d $$d/$$file; then \ 340 | dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \ 341 | if test -d "$(distdir)/$$file"; then \ 342 | find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ 343 | fi; \ 344 | if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ 345 | cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; \ 346 | find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ 347 | fi; \ 348 | cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; \ 349 | else \ 350 | test -f "$(distdir)/$$file" \ 351 | || cp -p $$d/$$file "$(distdir)/$$file" \ 352 | || exit 1; \ 353 | fi; \ 354 | done 355 | check-am: all-am 356 | check: check-am 357 | all-am: Makefile $(PROGRAMS) 358 | installdirs: 359 | for dir in "$(DESTDIR)$(bindir)"; do \ 360 | test -z "$$dir" || $(MKDIR_P) "$$dir"; \ 361 | done 362 | install: install-am 363 | install-exec: install-exec-am 364 | install-data: install-data-am 365 | uninstall: uninstall-am 366 | 367 | install-am: all-am 368 | @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am 369 | 370 | installcheck: installcheck-am 371 | install-strip: 372 | $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ 373 | install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ 374 | `test -z '$(STRIP)' || \ 375 | echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install 376 | mostlyclean-generic: 377 | 378 | clean-generic: 379 | 380 | distclean-generic: 381 | -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) 382 | -test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES) 383 | 384 | maintainer-clean-generic: 385 | @echo "This command is intended for maintainers to use" 386 | @echo "it deletes files that may require special tools to rebuild." 387 | clean: clean-am 388 | 389 | clean-am: clean-binPROGRAMS clean-generic mostlyclean-am 390 | 391 | distclean: distclean-am 392 | -rm -rf ./$(DEPDIR) 393 | -rm -f Makefile 394 | distclean-am: clean-am distclean-compile distclean-generic \ 395 | distclean-tags 396 | 397 | dvi: dvi-am 398 | 399 | dvi-am: 400 | 401 | html: html-am 402 | 403 | html-am: 404 | 405 | info: info-am 406 | 407 | info-am: 408 | 409 | install-data-am: 410 | 411 | install-dvi: install-dvi-am 412 | 413 | install-dvi-am: 414 | 415 | install-exec-am: install-binPROGRAMS 416 | 417 | install-html: install-html-am 418 | 419 | install-html-am: 420 | 421 | install-info: install-info-am 422 | 423 | install-info-am: 424 | 425 | install-man: 426 | 427 | install-pdf: install-pdf-am 428 | 429 | install-pdf-am: 430 | 431 | install-ps: install-ps-am 432 | 433 | install-ps-am: 434 | 435 | installcheck-am: 436 | 437 | maintainer-clean: maintainer-clean-am 438 | -rm -rf ./$(DEPDIR) 439 | -rm -f Makefile 440 | maintainer-clean-am: distclean-am maintainer-clean-generic 441 | 442 | mostlyclean: mostlyclean-am 443 | 444 | mostlyclean-am: mostlyclean-compile mostlyclean-generic 445 | 446 | pdf: pdf-am 447 | 448 | pdf-am: 449 | 450 | ps: ps-am 451 | 452 | ps-am: 453 | 454 | uninstall-am: uninstall-binPROGRAMS 455 | 456 | .MAKE: install-am install-strip 457 | 458 | .PHONY: CTAGS GTAGS all all-am check check-am clean clean-binPROGRAMS \ 459 | clean-generic ctags distclean distclean-compile \ 460 | distclean-generic distclean-tags distdir dvi dvi-am html \ 461 | html-am info info-am install install-am install-binPROGRAMS \ 462 | install-data install-data-am install-dvi install-dvi-am \ 463 | install-exec install-exec-am install-html install-html-am \ 464 | install-info install-info-am install-man install-pdf \ 465 | install-pdf-am install-ps install-ps-am install-strip \ 466 | installcheck installcheck-am installdirs maintainer-clean \ 467 | maintainer-clean-generic mostlyclean mostlyclean-compile \ 468 | mostlyclean-generic pdf pdf-am ps ps-am tags uninstall \ 469 | uninstall-am uninstall-binPROGRAMS 470 | 471 | 472 | # Tell versions [3.59,3.63) of GNU make to not export all variables. 473 | # Otherwise a system limit (for SysV at least) may be exceeded. 474 | .NOEXPORT: 475 | -------------------------------------------------------------------------------- /src/aes.h: -------------------------------------------------------------------------------- 1 | /* 2 | --------------------------------------------------------------------------- 3 | Copyright (c) 2003, Dr Brian Gladman , Worcester, UK. 4 | All rights reserved. 5 | 6 | LICENSE TERMS 7 | 8 | The free distribution and use of this software in both source and binary 9 | form is allowed (with or without changes) provided that: 10 | 11 | 1. distributions of this source code include the above copyright 12 | notice, this list of conditions and the following disclaimer; 13 | 14 | 2. distributions in binary form include the above copyright 15 | notice, this list of conditions and the following disclaimer 16 | in the documentation and/or other associated materials; 17 | 18 | 3. the copyright holder's name is not used to endorse products 19 | built using this software without specific written permission. 20 | 21 | ALTERNATIVELY, provided that this notice is retained in full, this product 22 | may be distributed under the terms of the GNU General Public License (GPL), 23 | in which case the provisions of the GPL apply INSTEAD OF those given above. 24 | 25 | DISCLAIMER 26 | 27 | This software is provided 'as is' with no explicit or implied warranties 28 | in respect of its properties, including, but not limited to, correctness 29 | and/or fitness for purpose. 30 | --------------------------------------------------------------------------- 31 | Issue Date: 1/05/2003 32 | 33 | This file contains the definitions required to use AES in C. See aesopt.h 34 | for optimisation details. 35 | */ 36 | 37 | #ifndef _AES_H 38 | #define _AES_H 39 | 40 | #if defined(__cplusplus) 41 | extern "C" 42 | { 43 | #endif 44 | 45 | #define AES_128 /* define if AES with 128 bit keys is needed */ 46 | #define AES_192 /* define if AES with 192 bit keys is needed */ 47 | #define AES_256 /* define if AES with 256 bit keys is needed */ 48 | #define AES_VAR /* define if a variable key size is needed */ 49 | 50 | /* The following must also be set in assembler files if being used */ 51 | 52 | #define AES_ENCRYPT /* define if support for encryption is needed */ 53 | #define AES_DECRYPT /* define if support for decryption is needed */ 54 | 55 | /* This include is used to find 8 & 32 bit unsigned integer types */ 56 | #include "limits.h" 57 | 58 | #if UCHAR_MAX == 0xff /* an unsigned 8 bit type */ 59 | typedef unsigned char aes_08t; 60 | #else 61 | #error Please define aes_08t as an 8-bit unsigned integer type in aes.h 62 | #endif 63 | 64 | #if UINT_MAX == 0xffffffff /* an unsigned 32 bit type */ 65 | typedef unsigned int aes_32t; 66 | #elif ULONG_MAX == 0xffffffff 67 | typedef unsigned long aes_32t; 68 | #else 69 | #error Please define aes_32t as a 32-bit unsigned integer type in aes.h 70 | #endif 71 | 72 | /* AES_BLOCK_SIZE is the size in BYTES of the AES block */ 73 | 74 | #define AES_BLOCK_SIZE 16 /* the AES block size */ 75 | #define KS_LENGTH (4 * AES_BLOCK_SIZE) /* key schedule length */ 76 | #define N_COLS (AES_BLOCK_SIZE >> 2) /* columns in the state */ 77 | 78 | #ifndef AES_DLL /* implement normal/DLL functions */ 79 | #define aes_rval void 80 | #else 81 | #define aes_rval void __declspec(dllexport) _stdcall 82 | #endif 83 | 84 | /* This routine must be called before first use if non-static */ 85 | /* tables are being used */ 86 | 87 | void gen_tabs(void); 88 | 89 | /* The key length (klen) is input in bytes when it is in the range */ 90 | /* 16 <= klen <= 32 or in bits when in the range 128 <= klen <= 256 */ 91 | 92 | #ifdef AES_ENCRYPT 93 | 94 | typedef struct /* AES context for encryption */ 95 | { aes_32t k_sch[KS_LENGTH]; /* the encryption key schedule */ 96 | aes_32t n_rnd; /* the number of cipher rounds */ 97 | } aes_encrypt_ctx; 98 | 99 | #if defined(AES_128) || defined(AES_VAR) 100 | aes_rval aes_encrypt_key128(const void *in_key, aes_encrypt_ctx cx[1]); 101 | #endif 102 | 103 | #if defined(AES_192) || defined(AES_VAR) 104 | aes_rval aes_encrypt_key192(const void *in_key, aes_encrypt_ctx cx[1]); 105 | #endif 106 | 107 | #if defined(AES_256) || defined(AES_VAR) 108 | aes_rval aes_encrypt_key256(const void *in_key, aes_encrypt_ctx cx[1]); 109 | #endif 110 | 111 | #if defined(AES_VAR) 112 | aes_rval aes_encrypt_key(const void *in_key, int key_len, aes_encrypt_ctx cx[1]); 113 | #endif 114 | 115 | aes_rval aes_encrypt(const void *in_blk, void *out_blk, const aes_encrypt_ctx cx[1]); 116 | #endif 117 | 118 | #ifdef AES_DECRYPT 119 | 120 | typedef struct /* AES context for decryption */ 121 | { aes_32t k_sch[KS_LENGTH]; /* the decryption key schedule */ 122 | aes_32t n_rnd; /* the number of cipher rounds */ 123 | } aes_decrypt_ctx; 124 | 125 | #if defined(AES_128) || defined(AES_VAR) 126 | aes_rval aes_decrypt_key128(const void *in_key, aes_decrypt_ctx cx[1]); 127 | #endif 128 | 129 | #if defined(AES_192) || defined(AES_VAR) 130 | aes_rval aes_decrypt_key192(const void *in_key, aes_decrypt_ctx cx[1]); 131 | #endif 132 | 133 | #if defined(AES_256) || defined(AES_VAR) 134 | aes_rval aes_decrypt_key256(const void *in_key, aes_decrypt_ctx cx[1]); 135 | #endif 136 | 137 | #if defined(AES_VAR) 138 | aes_rval aes_decrypt_key(const void *in_key, int key_len, aes_decrypt_ctx cx[1]); 139 | #endif 140 | 141 | aes_rval aes_decrypt(const void *in_blk, void *out_blk, const aes_decrypt_ctx cx[1]); 142 | #endif 143 | 144 | #if defined(__cplusplus) 145 | } 146 | #endif 147 | 148 | #endif 149 | -------------------------------------------------------------------------------- /src/aescrypt.c: -------------------------------------------------------------------------------- 1 | /* 2 | --------------------------------------------------------------------------- 3 | Copyright (c) 2003, Dr Brian Gladman , Worcester, UK. 4 | All rights reserved. 5 | 6 | LICENSE TERMS 7 | 8 | The free distribution and use of this software in both source and binary 9 | form is allowed (with or without changes) provided that: 10 | 11 | 1. distributions of this source code include the above copyright 12 | notice, this list of conditions and the following disclaimer; 13 | 14 | 2. distributions in binary form include the above copyright 15 | notice, this list of conditions and the following disclaimer 16 | in the documentation and/or other associated materials; 17 | 18 | 3. the copyright holder's name is not used to endorse products 19 | built using this software without specific written permission. 20 | 21 | ALTERNATIVELY, provided that this notice is retained in full, this product 22 | may be distributed under the terms of the GNU General Public License (GPL), 23 | in which case the provisions of the GPL apply INSTEAD OF those given above. 24 | 25 | DISCLAIMER 26 | 27 | This software is provided 'as is' with no explicit or implied warranties 28 | in respect of its properties, including, but not limited to, correctness 29 | and/or fitness for purpose. 30 | --------------------------------------------------------------------------- 31 | Issue Date: 1/05/2003 32 | 33 | This file contains the code for implementing encryption and decryption 34 | for AES (Rijndael) for block and key sizes of 16, 24 and 32 bytes. It 35 | can optionally be replaced by code written in assembler using NASM. For 36 | further details see the file aesopt.h 37 | */ 38 | 39 | #include "aesopt.h" 40 | 41 | #if defined(__cplusplus) 42 | extern "C" 43 | { 44 | #endif 45 | 46 | #define si(y,x,k,c) (s(y,c) = word_in(x, c) ^ k[c]) 47 | #define so(y,x,c) word_out(y, c, s(x,c)) 48 | 49 | #if defined(ARRAYS) 50 | #define locals(y,x) x[4],y[4] 51 | #else 52 | #define locals(y,x) x##0,x##1,x##2,x##3,y##0,y##1,y##2,y##3 53 | #endif 54 | 55 | #define l_copy(y, x) s(y,0) = s(x,0); s(y,1) = s(x,1); \ 56 | s(y,2) = s(x,2); s(y,3) = s(x,3); 57 | #define state_in(y,x,k) si(y,x,k,0); si(y,x,k,1); si(y,x,k,2); si(y,x,k,3) 58 | #define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); so(y,x,3) 59 | #define round(rm,y,x,k) rm(y,x,k,0); rm(y,x,k,1); rm(y,x,k,2); rm(y,x,k,3) 60 | 61 | #if defined(ENCRYPTION) && !defined(AES_ASM) 62 | 63 | /* Visual C++ .Net v7.1 provides the fastest encryption code when using 64 | Pentium optimiation with small code but this is poor for decryption 65 | so we need to control this with the following VC++ pragmas 66 | */ 67 | 68 | #if defined(_MSC_VER) 69 | #pragma optimize( "s", on ) 70 | #endif 71 | 72 | /* Given the column (c) of the output state variable, the following 73 | macros give the input state variables which are needed in its 74 | computation for each row (r) of the state. All the alternative 75 | macros give the same end values but expand into different ways 76 | of calculating these values. In particular the complex macro 77 | used for dynamically variable block sizes is designed to expand 78 | to a compile time constant whenever possible but will expand to 79 | conditional clauses on some branches (I am grateful to Frank 80 | Yellin for this construction) 81 | */ 82 | 83 | #define fwd_var(x,r,c)\ 84 | ( r == 0 ? ( c == 0 ? s(x,0) : c == 1 ? s(x,1) : c == 2 ? s(x,2) : s(x,3))\ 85 | : r == 1 ? ( c == 0 ? s(x,1) : c == 1 ? s(x,2) : c == 2 ? s(x,3) : s(x,0))\ 86 | : r == 2 ? ( c == 0 ? s(x,2) : c == 1 ? s(x,3) : c == 2 ? s(x,0) : s(x,1))\ 87 | : ( c == 0 ? s(x,3) : c == 1 ? s(x,0) : c == 2 ? s(x,1) : s(x,2))) 88 | 89 | #if defined(FT4_SET) 90 | #undef dec_fmvars 91 | #define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,n),fwd_var,rf1,c)) 92 | #elif defined(FT1_SET) 93 | #undef dec_fmvars 94 | #define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,upr,t_use(f,n),fwd_var,rf1,c)) 95 | #else 96 | #define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ fwd_mcol(no_table(x,t_use(s,box),fwd_var,rf1,c))) 97 | #endif 98 | 99 | #if defined(FL4_SET) 100 | #define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,l),fwd_var,rf1,c)) 101 | #elif defined(FL1_SET) 102 | #define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,ups,t_use(f,l),fwd_var,rf1,c)) 103 | #else 104 | #define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ no_table(x,t_use(s,box),fwd_var,rf1,c)) 105 | #endif 106 | 107 | aes_rval aes_encrypt(const void *in_blk, void *out_blk, const aes_encrypt_ctx cx[1]) 108 | { aes_32t locals(b0, b1); 109 | const aes_32t *kp = cx->k_sch; 110 | #ifdef dec_fmvars 111 | dec_fmvars; /* declare variables for fwd_mcol() if needed */ 112 | #endif 113 | 114 | state_in(b0, in_blk, kp); 115 | 116 | #if (ENC_UNROLL == FULL) 117 | 118 | kp += (cx->n_rnd - 9) * N_COLS; 119 | 120 | /*lint -e{616} control flows into case/default */ 121 | switch(cx->n_rnd) 122 | { 123 | case 14: 124 | round(fwd_rnd, b1, b0, kp - 4 * N_COLS); 125 | round(fwd_rnd, b0, b1, kp - 3 * N_COLS); 126 | case 12: 127 | round(fwd_rnd, b1, b0, kp - 2 * N_COLS); 128 | round(fwd_rnd, b0, b1, kp - N_COLS); 129 | case 10: 130 | round(fwd_rnd, b1, b0, kp ); 131 | round(fwd_rnd, b0, b1, kp + N_COLS); 132 | round(fwd_rnd, b1, b0, kp + 2 * N_COLS); 133 | round(fwd_rnd, b0, b1, kp + 3 * N_COLS); 134 | round(fwd_rnd, b1, b0, kp + 4 * N_COLS); 135 | round(fwd_rnd, b0, b1, kp + 5 * N_COLS); 136 | round(fwd_rnd, b1, b0, kp + 6 * N_COLS); 137 | round(fwd_rnd, b0, b1, kp + 7 * N_COLS); 138 | round(fwd_rnd, b1, b0, kp + 8 * N_COLS); 139 | round(fwd_lrnd, b0, b1, kp + 9 * N_COLS); 140 | default: 141 | ; 142 | } 143 | #else 144 | 145 | #if (ENC_UNROLL == PARTIAL) 146 | { aes_32t rnd; 147 | for(rnd = 0; rnd < (cx->n_rnd >> 1) - 1; ++rnd) 148 | { 149 | kp += N_COLS; 150 | round(fwd_rnd, b1, b0, kp); 151 | kp += N_COLS; 152 | round(fwd_rnd, b0, b1, kp); 153 | } 154 | kp += N_COLS; 155 | round(fwd_rnd, b1, b0, kp); 156 | #else 157 | { aes_32t rnd, *p0 = b0, *p1 = b1, *pt; 158 | for(rnd = 0; rnd < cx->n_rnd - 1; ++rnd) 159 | { 160 | kp += N_COLS; 161 | round(fwd_rnd, p1, p0, kp); 162 | pt = p0, p0 = p1, p1 = pt; 163 | } 164 | #endif 165 | kp += N_COLS; 166 | round(fwd_lrnd, b0, b1, kp); 167 | } 168 | #endif 169 | 170 | state_out(out_blk, b0); 171 | } 172 | 173 | #endif 174 | 175 | #if defined(DECRYPTION) && !defined(AES_ASM) 176 | 177 | /* Visual C++ .Net v7.1 provides the fastest encryption code when using 178 | Pentium optimiation with small code but this is poor for decryption 179 | so we need to control this with the following VC++ pragmas 180 | */ 181 | 182 | #if defined(_MSC_VER) 183 | #pragma optimize( "t", on ) 184 | #endif 185 | 186 | /* Given the column (c) of the output state variable, the following 187 | macros give the input state variables which are needed in its 188 | computation for each row (r) of the state. All the alternative 189 | macros give the same end values but expand into different ways 190 | of calculating these values. In particular the complex macro 191 | used for dynamically variable block sizes is designed to expand 192 | to a compile time constant whenever possible but will expand to 193 | conditional clauses on some branches (I am grateful to Frank 194 | Yellin for this construction) 195 | */ 196 | 197 | #define inv_var(x,r,c)\ 198 | ( r == 0 ? ( c == 0 ? s(x,0) : c == 1 ? s(x,1) : c == 2 ? s(x,2) : s(x,3))\ 199 | : r == 1 ? ( c == 0 ? s(x,3) : c == 1 ? s(x,0) : c == 2 ? s(x,1) : s(x,2))\ 200 | : r == 2 ? ( c == 0 ? s(x,2) : c == 1 ? s(x,3) : c == 2 ? s(x,0) : s(x,1))\ 201 | : ( c == 0 ? s(x,1) : c == 1 ? s(x,2) : c == 2 ? s(x,3) : s(x,0))) 202 | 203 | #if defined(IT4_SET) 204 | #undef dec_imvars 205 | #define inv_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(i,n),inv_var,rf1,c)) 206 | #elif defined(IT1_SET) 207 | #undef dec_imvars 208 | #define inv_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,upr,t_use(i,n),inv_var,rf1,c)) 209 | #else 210 | #define inv_rnd(y,x,k,c) (s(y,c) = inv_mcol((k)[c] ^ no_table(x,t_use(i,box),inv_var,rf1,c))) 211 | #endif 212 | 213 | #if defined(IL4_SET) 214 | #define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(i,l),inv_var,rf1,c)) 215 | #elif defined(IL1_SET) 216 | #define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,ups,t_use(i,l),inv_var,rf1,c)) 217 | #else 218 | #define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ no_table(x,t_use(i,box),inv_var,rf1,c)) 219 | #endif 220 | 221 | aes_rval aes_decrypt(const void *in_blk, void *out_blk, const aes_decrypt_ctx cx[1]) 222 | { aes_32t locals(b0, b1); 223 | const aes_32t *kp = cx->k_sch + N_COLS * cx->n_rnd; 224 | #ifdef dec_imvars 225 | dec_imvars; /* declare variables for inv_mcol() if needed */ 226 | #endif 227 | 228 | state_in(b0, in_blk, kp); 229 | 230 | #if (DEC_UNROLL == FULL) 231 | 232 | kp = cx->k_sch + 9 * N_COLS; 233 | 234 | /*lint -e{616} control flows into case/default */ 235 | switch(cx->n_rnd) 236 | { 237 | case 14: 238 | round(inv_rnd, b1, b0, kp + 4 * N_COLS); 239 | round(inv_rnd, b0, b1, kp + 3 * N_COLS); 240 | case 12: 241 | round(inv_rnd, b1, b0, kp + 2 * N_COLS); 242 | round(inv_rnd, b0, b1, kp + N_COLS ); 243 | case 10: 244 | round(inv_rnd, b1, b0, kp ); 245 | round(inv_rnd, b0, b1, kp - N_COLS); 246 | round(inv_rnd, b1, b0, kp - 2 * N_COLS); 247 | round(inv_rnd, b0, b1, kp - 3 * N_COLS); 248 | round(inv_rnd, b1, b0, kp - 4 * N_COLS); 249 | round(inv_rnd, b0, b1, kp - 5 * N_COLS); 250 | round(inv_rnd, b1, b0, kp - 6 * N_COLS); 251 | round(inv_rnd, b0, b1, kp - 7 * N_COLS); 252 | round(inv_rnd, b1, b0, kp - 8 * N_COLS); 253 | round(inv_lrnd, b0, b1, kp - 9 * N_COLS); 254 | default: 255 | ; 256 | } 257 | #else 258 | 259 | #if (DEC_UNROLL == PARTIAL) 260 | { aes_32t rnd; 261 | for(rnd = 0; rnd < (cx->n_rnd >> 1) - 1; ++rnd) 262 | { 263 | kp -= N_COLS; 264 | round(inv_rnd, b1, b0, kp); 265 | kp -= N_COLS; 266 | round(inv_rnd, b0, b1, kp); 267 | } 268 | kp -= N_COLS; 269 | round(inv_rnd, b1, b0, kp); 270 | #else 271 | { aes_32t rnd, *p0 = b0, *p1 = b1, *pt; 272 | for(rnd = 0; rnd < cx->n_rnd - 1; ++rnd) 273 | { 274 | kp -= N_COLS; 275 | round(inv_rnd, p1, p0, kp); 276 | pt = p0, p0 = p1, p1 = pt; 277 | } 278 | #endif 279 | kp -= N_COLS; 280 | round(inv_lrnd, b0, b1, kp); 281 | } 282 | #endif 283 | 284 | state_out(out_blk, b0); 285 | } 286 | 287 | #endif 288 | 289 | #if defined(__cplusplus) 290 | } 291 | #endif 292 | -------------------------------------------------------------------------------- /src/aeskey.c: -------------------------------------------------------------------------------- 1 | /* 2 | --------------------------------------------------------------------------- 3 | Copyright (c) 2003, Dr Brian Gladman , Worcester, UK. 4 | All rights reserved. 5 | 6 | LICENSE TERMS 7 | 8 | The free distribution and use of this software in both source and binary 9 | form is allowed (with or without changes) provided that: 10 | 11 | 1. distributions of this source code include the above copyright 12 | notice, this list of conditions and the following disclaimer; 13 | 14 | 2. distributions in binary form include the above copyright 15 | notice, this list of conditions and the following disclaimer 16 | in the documentation and/or other associated materials; 17 | 18 | 3. the copyright holder's name is not used to endorse products 19 | built using this software without specific written permission. 20 | 21 | ALTERNATIVELY, provided that this notice is retained in full, this product 22 | may be distributed under the terms of the GNU General Public License (GPL), 23 | in which case the provisions of the GPL apply INSTEAD OF those given above. 24 | 25 | DISCLAIMER 26 | 27 | This software is provided 'as is' with no explicit or implied warranties 28 | in respect of its properties, including, but not limited to, correctness 29 | and/or fitness for purpose. 30 | --------------------------------------------------------------------------- 31 | Issue Date: 1/05/2003 32 | 33 | This file contains the code for implementing the key schedule for AES 34 | (Rijndael) for block and key sizes of 16, 24, and 32 bytes. See aesopt.h 35 | for further details including optimisation. 36 | */ 37 | 38 | #include "aesopt.h" 39 | 40 | #if defined(__cplusplus) 41 | extern "C" 42 | { 43 | #endif 44 | 45 | /* Initialise the key schedule from the user supplied key. The key 46 | length can be specified in bytes, with legal values of 16, 24 47 | and 32, or in bits, with legal values of 128, 192 and 256. These 48 | values correspond with Nk values of 4, 6 and 8 respectively. 49 | 50 | The following macros implement a single cycle in the key 51 | schedule generation process. The number of cycles needed 52 | for each cx->n_col and nk value is: 53 | 54 | nk = 4 5 6 7 8 55 | ------------------------------ 56 | cx->n_col = 4 10 9 8 7 7 57 | cx->n_col = 5 14 11 10 9 9 58 | cx->n_col = 6 19 15 12 11 11 59 | cx->n_col = 7 21 19 16 13 14 60 | cx->n_col = 8 29 23 19 17 14 61 | */ 62 | 63 | #define ke4(k,i) \ 64 | { k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[4*(i)+5] = ss[1] ^= ss[0]; \ 65 | k[4*(i)+6] = ss[2] ^= ss[1]; k[4*(i)+7] = ss[3] ^= ss[2]; \ 66 | } 67 | #define kel4(k,i) \ 68 | { k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[4*(i)+5] = ss[1] ^= ss[0]; \ 69 | k[4*(i)+6] = ss[2] ^= ss[1]; k[4*(i)+7] = ss[3] ^= ss[2]; \ 70 | } 71 | 72 | #define ke6(k,i) \ 73 | { k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[6*(i)+ 7] = ss[1] ^= ss[0]; \ 74 | k[6*(i)+ 8] = ss[2] ^= ss[1]; k[6*(i)+ 9] = ss[3] ^= ss[2]; \ 75 | k[6*(i)+10] = ss[4] ^= ss[3]; k[6*(i)+11] = ss[5] ^= ss[4]; \ 76 | } 77 | #define kel6(k,i) \ 78 | { k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[6*(i)+ 7] = ss[1] ^= ss[0]; \ 79 | k[6*(i)+ 8] = ss[2] ^= ss[1]; k[6*(i)+ 9] = ss[3] ^= ss[2]; \ 80 | } 81 | 82 | #define ke8(k,i) \ 83 | { k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[8*(i)+ 9] = ss[1] ^= ss[0]; \ 84 | k[8*(i)+10] = ss[2] ^= ss[1]; k[8*(i)+11] = ss[3] ^= ss[2]; \ 85 | k[8*(i)+12] = ss[4] ^= ls_box(ss[3],0); k[8*(i)+13] = ss[5] ^= ss[4]; \ 86 | k[8*(i)+14] = ss[6] ^= ss[5]; k[8*(i)+15] = ss[7] ^= ss[6]; \ 87 | } 88 | #define kel8(k,i) \ 89 | { k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[8*(i)+ 9] = ss[1] ^= ss[0]; \ 90 | k[8*(i)+10] = ss[2] ^= ss[1]; k[8*(i)+11] = ss[3] ^= ss[2]; \ 91 | } 92 | 93 | #if defined(ENCRYPTION_KEY_SCHEDULE) 94 | 95 | #if defined(AES_128) || defined(AES_VAR) 96 | 97 | aes_rval aes_encrypt_key128(const void *in_key, aes_encrypt_ctx cx[1]) 98 | { aes_32t ss[4]; 99 | 100 | cx->n_rnd = 10; 101 | cx->k_sch[0] = ss[0] = word_in(in_key, 0); 102 | cx->k_sch[1] = ss[1] = word_in(in_key, 1); 103 | cx->k_sch[2] = ss[2] = word_in(in_key, 2); 104 | cx->k_sch[3] = ss[3] = word_in(in_key, 3); 105 | 106 | #if ENC_UNROLL == NONE 107 | { aes_32t i; 108 | 109 | for(i = 0; i < ((11 * N_COLS - 1) / 4); ++i) 110 | ke4(cx->k_sch, i); 111 | } 112 | #else 113 | ke4(cx->k_sch, 0); ke4(cx->k_sch, 1); 114 | ke4(cx->k_sch, 2); ke4(cx->k_sch, 3); 115 | ke4(cx->k_sch, 4); ke4(cx->k_sch, 5); 116 | ke4(cx->k_sch, 6); ke4(cx->k_sch, 7); 117 | ke4(cx->k_sch, 8); kel4(cx->k_sch, 9); 118 | #endif 119 | } 120 | 121 | #endif 122 | 123 | #if defined(AES_192) || defined(AES_VAR) 124 | 125 | aes_rval aes_encrypt_key192(const void *in_key, aes_encrypt_ctx cx[1]) 126 | { aes_32t ss[6]; 127 | 128 | cx->n_rnd = 12; 129 | cx->k_sch[0] = ss[0] = word_in(in_key, 0); 130 | cx->k_sch[1] = ss[1] = word_in(in_key, 1); 131 | cx->k_sch[2] = ss[2] = word_in(in_key, 2); 132 | cx->k_sch[3] = ss[3] = word_in(in_key, 3); 133 | cx->k_sch[4] = ss[4] = word_in(in_key, 4); 134 | cx->k_sch[5] = ss[5] = word_in(in_key, 5); 135 | 136 | #if ENC_UNROLL == NONE 137 | { aes_32t i; 138 | 139 | for(i = 0; i < (13 * N_COLS - 1) / 6; ++i) 140 | ke6(cx->k_sch, i); 141 | } 142 | #else 143 | ke6(cx->k_sch, 0); ke6(cx->k_sch, 1); 144 | ke6(cx->k_sch, 2); ke6(cx->k_sch, 3); 145 | ke6(cx->k_sch, 4); ke6(cx->k_sch, 5); 146 | ke6(cx->k_sch, 6); kel6(cx->k_sch, 7); 147 | #endif 148 | } 149 | 150 | #endif 151 | 152 | #if defined(AES_256) || defined(AES_VAR) 153 | 154 | aes_rval aes_encrypt_key256(const void *in_key, aes_encrypt_ctx cx[1]) 155 | { aes_32t ss[8]; 156 | 157 | cx->n_rnd = 14; 158 | cx->k_sch[0] = ss[0] = word_in(in_key, 0); 159 | cx->k_sch[1] = ss[1] = word_in(in_key, 1); 160 | cx->k_sch[2] = ss[2] = word_in(in_key, 2); 161 | cx->k_sch[3] = ss[3] = word_in(in_key, 3); 162 | cx->k_sch[4] = ss[4] = word_in(in_key, 4); 163 | cx->k_sch[5] = ss[5] = word_in(in_key, 5); 164 | cx->k_sch[6] = ss[6] = word_in(in_key, 6); 165 | cx->k_sch[7] = ss[7] = word_in(in_key, 7); 166 | 167 | #if ENC_UNROLL == NONE 168 | { aes_32t i; 169 | 170 | for(i = 0; i < (15 * N_COLS - 1) / 8; ++i) 171 | ke8(cx->k_sch, i); 172 | } 173 | #else 174 | ke8(cx->k_sch, 0); ke8(cx->k_sch, 1); 175 | ke8(cx->k_sch, 2); ke8(cx->k_sch, 3); 176 | ke8(cx->k_sch, 4); ke8(cx->k_sch, 5); 177 | kel8(cx->k_sch, 6); 178 | #endif 179 | } 180 | 181 | #endif 182 | 183 | #if defined(AES_VAR) 184 | 185 | aes_rval aes_encrypt_key(const void *in_key, int key_len, aes_encrypt_ctx cx[1]) 186 | { 187 | switch(key_len) 188 | { 189 | case 16: case 128: aes_encrypt_key128(in_key, cx); return; 190 | case 24: case 192: aes_encrypt_key192(in_key, cx); return; 191 | case 32: case 256: aes_encrypt_key256(in_key, cx); return; 192 | default: return; 193 | } 194 | } 195 | 196 | #endif 197 | 198 | #endif 199 | 200 | #if defined(DECRYPTION_KEY_SCHEDULE) 201 | 202 | #if DEC_ROUND == NO_TABLES 203 | #define ff(x) (x) 204 | #else 205 | #define ff(x) inv_mcol(x) 206 | #ifdef dec_imvars 207 | #define d_vars dec_imvars 208 | #endif 209 | #endif 210 | 211 | #if 1 212 | #define kdf4(k,i) \ 213 | { ss[0] = ss[0] ^ ss[2] ^ ss[1] ^ ss[3]; ss[1] = ss[1] ^ ss[3]; ss[2] = ss[2] ^ ss[3]; ss[3] = ss[3]; \ 214 | ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; ss[i % 4] ^= ss[4]; \ 215 | ss[4] ^= k[4*(i)]; k[4*(i)+4] = ff(ss[4]); ss[4] ^= k[4*(i)+1]; k[4*(i)+5] = ff(ss[4]); \ 216 | ss[4] ^= k[4*(i)+2]; k[4*(i)+6] = ff(ss[4]); ss[4] ^= k[4*(i)+3]; k[4*(i)+7] = ff(ss[4]); \ 217 | } 218 | #define kd4(k,i) \ 219 | { ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; ss[i % 4] ^= ss[4]; ss[4] = ff(ss[4]); \ 220 | k[4*(i)+4] = ss[4] ^= k[4*(i)]; k[4*(i)+5] = ss[4] ^= k[4*(i)+1]; \ 221 | k[4*(i)+6] = ss[4] ^= k[4*(i)+2]; k[4*(i)+7] = ss[4] ^= k[4*(i)+3]; \ 222 | } 223 | #define kdl4(k,i) \ 224 | { ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; ss[i % 4] ^= ss[4]; \ 225 | k[4*(i)+4] = (ss[0] ^= ss[1]) ^ ss[2] ^ ss[3]; k[4*(i)+5] = ss[1] ^ ss[3]; \ 226 | k[4*(i)+6] = ss[0]; k[4*(i)+7] = ss[1]; \ 227 | } 228 | #else 229 | #define kdf4(k,i) \ 230 | { ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[4*(i)+ 4] = ff(ss[0]); ss[1] ^= ss[0]; k[4*(i)+ 5] = ff(ss[1]); \ 231 | ss[2] ^= ss[1]; k[4*(i)+ 6] = ff(ss[2]); ss[3] ^= ss[2]; k[4*(i)+ 7] = ff(ss[3]); \ 232 | } 233 | #define kd4(k,i) \ 234 | { ss[4] = ls_box(ss[3],3) ^ t_use(r,c)[i]; \ 235 | ss[0] ^= ss[4]; ss[4] = ff(ss[4]); k[4*(i)+ 4] = ss[4] ^= k[4*(i)]; \ 236 | ss[1] ^= ss[0]; k[4*(i)+ 5] = ss[4] ^= k[4*(i)+ 1]; \ 237 | ss[2] ^= ss[1]; k[4*(i)+ 6] = ss[4] ^= k[4*(i)+ 2]; \ 238 | ss[3] ^= ss[2]; k[4*(i)+ 7] = ss[4] ^= k[4*(i)+ 3]; \ 239 | } 240 | #define kdl4(k,i) \ 241 | { ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[4*(i)+ 4] = ss[0]; ss[1] ^= ss[0]; k[4*(i)+ 5] = ss[1]; \ 242 | ss[2] ^= ss[1]; k[4*(i)+ 6] = ss[2]; ss[3] ^= ss[2]; k[4*(i)+ 7] = ss[3]; \ 243 | } 244 | #endif 245 | 246 | #define kdf6(k,i) \ 247 | { ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[6*(i)+ 6] = ff(ss[0]); ss[1] ^= ss[0]; k[6*(i)+ 7] = ff(ss[1]); \ 248 | ss[2] ^= ss[1]; k[6*(i)+ 8] = ff(ss[2]); ss[3] ^= ss[2]; k[6*(i)+ 9] = ff(ss[3]); \ 249 | ss[4] ^= ss[3]; k[6*(i)+10] = ff(ss[4]); ss[5] ^= ss[4]; k[6*(i)+11] = ff(ss[5]); \ 250 | } 251 | #define kd6(k,i) \ 252 | { ss[6] = ls_box(ss[5],3) ^ t_use(r,c)[i]; \ 253 | ss[0] ^= ss[6]; ss[6] = ff(ss[6]); k[6*(i)+ 6] = ss[6] ^= k[6*(i)]; \ 254 | ss[1] ^= ss[0]; k[6*(i)+ 7] = ss[6] ^= k[6*(i)+ 1]; \ 255 | ss[2] ^= ss[1]; k[6*(i)+ 8] = ss[6] ^= k[6*(i)+ 2]; \ 256 | ss[3] ^= ss[2]; k[6*(i)+ 9] = ss[6] ^= k[6*(i)+ 3]; \ 257 | ss[4] ^= ss[3]; k[6*(i)+10] = ss[6] ^= k[6*(i)+ 4]; \ 258 | ss[5] ^= ss[4]; k[6*(i)+11] = ss[6] ^= k[6*(i)+ 5]; \ 259 | } 260 | #define kdl6(k,i) \ 261 | { ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[6*(i)+ 6] = ss[0]; ss[1] ^= ss[0]; k[6*(i)+ 7] = ss[1]; \ 262 | ss[2] ^= ss[1]; k[6*(i)+ 8] = ss[2]; ss[3] ^= ss[2]; k[6*(i)+ 9] = ss[3]; \ 263 | } 264 | 265 | #define kdf8(k,i) \ 266 | { ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[8*(i)+ 8] = ff(ss[0]); ss[1] ^= ss[0]; k[8*(i)+ 9] = ff(ss[1]); \ 267 | ss[2] ^= ss[1]; k[8*(i)+10] = ff(ss[2]); ss[3] ^= ss[2]; k[8*(i)+11] = ff(ss[3]); \ 268 | ss[4] ^= ls_box(ss[3],0); k[8*(i)+12] = ff(ss[4]); ss[5] ^= ss[4]; k[8*(i)+13] = ff(ss[5]); \ 269 | ss[6] ^= ss[5]; k[8*(i)+14] = ff(ss[6]); ss[7] ^= ss[6]; k[8*(i)+15] = ff(ss[7]); \ 270 | } 271 | #define kd8(k,i) \ 272 | { aes_32t g = ls_box(ss[7],3) ^ t_use(r,c)[i]; \ 273 | ss[0] ^= g; g = ff(g); k[8*(i)+ 8] = g ^= k[8*(i)]; \ 274 | ss[1] ^= ss[0]; k[8*(i)+ 9] = g ^= k[8*(i)+ 1]; \ 275 | ss[2] ^= ss[1]; k[8*(i)+10] = g ^= k[8*(i)+ 2]; \ 276 | ss[3] ^= ss[2]; k[8*(i)+11] = g ^= k[8*(i)+ 3]; \ 277 | g = ls_box(ss[3],0); \ 278 | ss[4] ^= g; g = ff(g); k[8*(i)+12] = g ^= k[8*(i)+ 4]; \ 279 | ss[5] ^= ss[4]; k[8*(i)+13] = g ^= k[8*(i)+ 5]; \ 280 | ss[6] ^= ss[5]; k[8*(i)+14] = g ^= k[8*(i)+ 6]; \ 281 | ss[7] ^= ss[6]; k[8*(i)+15] = g ^= k[8*(i)+ 7]; \ 282 | } 283 | #define kdl8(k,i) \ 284 | { ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[8*(i)+ 8] = ss[0]; ss[1] ^= ss[0]; k[8*(i)+ 9] = ss[1]; \ 285 | ss[2] ^= ss[1]; k[8*(i)+10] = ss[2]; ss[3] ^= ss[2]; k[8*(i)+11] = ss[3]; \ 286 | } 287 | 288 | #if defined(AES_128) || defined(AES_VAR) 289 | 290 | aes_rval aes_decrypt_key128(const void *in_key, aes_decrypt_ctx cx[1]) 291 | { aes_32t ss[5]; 292 | #ifdef d_vars 293 | d_vars; 294 | #endif 295 | cx->n_rnd = 10; 296 | 297 | cx->k_sch[0] = ss[0] = word_in(in_key, 0); 298 | cx->k_sch[1] = ss[1] = word_in(in_key, 1); 299 | cx->k_sch[2] = ss[2] = word_in(in_key, 2); 300 | cx->k_sch[3] = ss[3] = word_in(in_key, 3); 301 | 302 | #if DEC_UNROLL == NONE 303 | { aes_32t i; 304 | 305 | for(i = 0; i < (11 * N_COLS - 1) / 4; ++i) 306 | ke4(cx->k_sch, i); 307 | #if !(DEC_ROUND == NO_TABLES) 308 | for(i = N_COLS; i < 10 * N_COLS; ++i) 309 | cx->k_sch[i] = inv_mcol(cx->k_sch[i]); 310 | #endif 311 | } 312 | #else 313 | kdf4(cx->k_sch, 0); kd4(cx->k_sch, 1); 314 | kd4(cx->k_sch, 2); kd4(cx->k_sch, 3); 315 | kd4(cx->k_sch, 4); kd4(cx->k_sch, 5); 316 | kd4(cx->k_sch, 6); kd4(cx->k_sch, 7); 317 | kd4(cx->k_sch, 8); kdl4(cx->k_sch, 9); 318 | #endif 319 | } 320 | 321 | #endif 322 | 323 | #if defined(AES_192) || defined(AES_VAR) 324 | 325 | aes_rval aes_decrypt_key192(const void *in_key, aes_decrypt_ctx cx[1]) 326 | { aes_32t ss[7]; 327 | #ifdef d_vars 328 | d_vars; 329 | #endif 330 | cx->n_rnd = 12; 331 | 332 | cx->k_sch[0] = ss[0] = word_in(in_key, 0); 333 | cx->k_sch[1] = ss[1] = word_in(in_key, 1); 334 | cx->k_sch[2] = ss[2] = word_in(in_key, 2); 335 | cx->k_sch[3] = ss[3] = word_in(in_key, 3); 336 | 337 | #if DEC_UNROLL == NONE 338 | cx->k_sch[4] = ss[4] = word_in(in_key, 4); 339 | cx->k_sch[5] = ss[5] = word_in(in_key, 5); 340 | { aes_32t i; 341 | 342 | for(i = 0; i < (13 * N_COLS - 1) / 6; ++i) 343 | ke6(cx->k_sch, i); 344 | #if !(DEC_ROUND == NO_TABLES) 345 | for(i = N_COLS; i < 12 * N_COLS; ++i) 346 | cx->k_sch[i] = inv_mcol(cx->k_sch[i]); 347 | #endif 348 | } 349 | #else 350 | cx->k_sch[4] = ff(ss[4] = word_in(in_key, 4)); 351 | cx->k_sch[5] = ff(ss[5] = word_in(in_key, 5)); 352 | kdf6(cx->k_sch, 0); kd6(cx->k_sch, 1); 353 | kd6(cx->k_sch, 2); kd6(cx->k_sch, 3); 354 | kd6(cx->k_sch, 4); kd6(cx->k_sch, 5); 355 | kd6(cx->k_sch, 6); kdl6(cx->k_sch, 7); 356 | #endif 357 | } 358 | 359 | #endif 360 | 361 | #if defined(AES_256) || defined(AES_VAR) 362 | 363 | aes_rval aes_decrypt_key256(const void *in_key, aes_decrypt_ctx cx[1]) 364 | { aes_32t ss[8]; 365 | #ifdef d_vars 366 | d_vars; 367 | #endif 368 | cx->n_rnd = 14; 369 | 370 | cx->k_sch[0] = ss[0] = word_in(in_key, 0); 371 | cx->k_sch[1] = ss[1] = word_in(in_key, 1); 372 | cx->k_sch[2] = ss[2] = word_in(in_key, 2); 373 | cx->k_sch[3] = ss[3] = word_in(in_key, 3); 374 | 375 | #if DEC_UNROLL == NONE 376 | cx->k_sch[4] = ss[4] = word_in(in_key, 4); 377 | cx->k_sch[5] = ss[5] = word_in(in_key, 5); 378 | cx->k_sch[6] = ss[6] = word_in(in_key, 6); 379 | cx->k_sch[7] = ss[7] = word_in(in_key, 7); 380 | { aes_32t i; 381 | 382 | for(i = 0; i < (15 * N_COLS - 1) / 8; ++i) 383 | ke8(cx->k_sch, i); 384 | #if !(DEC_ROUND == NO_TABLES) 385 | for(i = N_COLS; i < 14 * N_COLS; ++i) 386 | cx->k_sch[i] = inv_mcol(cx->k_sch[i]); 387 | #endif 388 | } 389 | #else 390 | cx->k_sch[4] = ff(ss[4] = word_in(in_key, 4)); 391 | cx->k_sch[5] = ff(ss[5] = word_in(in_key, 5)); 392 | cx->k_sch[6] = ff(ss[6] = word_in(in_key, 6)); 393 | cx->k_sch[7] = ff(ss[7] = word_in(in_key, 7)); 394 | kdf8(cx->k_sch, 0); kd8(cx->k_sch, 1); 395 | kd8(cx->k_sch, 2); kd8(cx->k_sch, 3); 396 | kd8(cx->k_sch, 4); kd8(cx->k_sch, 5); 397 | kdl8(cx->k_sch, 6); 398 | #endif 399 | } 400 | 401 | #endif 402 | 403 | #if defined(AES_VAR) 404 | 405 | aes_rval aes_decrypt_key(const void *in_key, int key_len, aes_decrypt_ctx cx[1]) 406 | { 407 | switch(key_len) 408 | { 409 | case 16: case 128: aes_decrypt_key128(in_key, cx); return; 410 | case 24: case 192: aes_decrypt_key192(in_key, cx); return; 411 | case 32: case 256: aes_decrypt_key256(in_key, cx); return; 412 | default: return; 413 | } 414 | } 415 | 416 | #endif 417 | 418 | #endif 419 | 420 | #if defined(__cplusplus) 421 | } 422 | #endif 423 | -------------------------------------------------------------------------------- /src/aestab.c: -------------------------------------------------------------------------------- 1 | /* 2 | --------------------------------------------------------------------------- 3 | Copyright (c) 2003, Dr Brian Gladman , Worcester, UK. 4 | All rights reserved. 5 | 6 | LICENSE TERMS 7 | 8 | The free distribution and use of this software in both source and binary 9 | form is allowed (with or without changes) provided that: 10 | 11 | 1. distributions of this source code include the above copyright 12 | notice, this list of conditions and the following disclaimer; 13 | 14 | 2. distributions in binary form include the above copyright 15 | notice, this list of conditions and the following disclaimer 16 | in the documentation and/or other associated materials; 17 | 18 | 3. the copyright holder's name is not used to endorse products 19 | built using this software without specific written permission. 20 | 21 | ALTERNATIVELY, provided that this notice is retained in full, this product 22 | may be distributed under the terms of the GNU General Public License (GPL), 23 | in which case the provisions of the GPL apply INSTEAD OF those given above. 24 | 25 | DISCLAIMER 26 | 27 | This software is provided 'as is' with no explicit or implied warranties 28 | in respect of its properties, including, but not limited to, correctness 29 | and/or fitness for purpose. 30 | --------------------------------------------------------------------------- 31 | Issue Date: 1/05/2003 32 | 33 | */ 34 | 35 | #if defined(__cplusplus) 36 | extern "C" 37 | { 38 | #endif 39 | 40 | #define DO_TABLES 41 | 42 | #include "aesopt.h" 43 | 44 | #if defined(FIXED_TABLES) 45 | 46 | /* implemented in case of wrong call for fixed tables */ 47 | 48 | void gen_tabs(void) 49 | { 50 | } 51 | 52 | #else /* dynamic table generation */ 53 | 54 | #if !defined(FF_TABLES) 55 | 56 | /* Generate the tables for the dynamic table option 57 | 58 | It will generally be sensible to use tables to compute finite 59 | field multiplies and inverses but where memory is scarse this 60 | code might sometimes be better. But it only has effect during 61 | initialisation so its pretty unimportant in overall terms. 62 | */ 63 | 64 | /* return 2 ^ (n - 1) where n is the bit number of the highest bit 65 | set in x with x in the range 1 < x < 0x00000200. This form is 66 | used so that locals within fi can be bytes rather than words 67 | */ 68 | 69 | static aes_08t hibit(const aes_32t x) 70 | { aes_08t r = (aes_08t)((x >> 1) | (x >> 2)); 71 | 72 | r |= (r >> 2); 73 | r |= (r >> 4); 74 | return (r + 1) >> 1; 75 | } 76 | 77 | /* return the inverse of the finite field element x */ 78 | 79 | static aes_08t fi(const aes_08t x) 80 | { aes_08t p1 = x, p2 = BPOLY, n1 = hibit(x), n2 = 0x80, v1 = 1, v2 = 0; 81 | 82 | if(x < 2) return x; 83 | 84 | for(;;) 85 | { 86 | if(!n1) return v1; 87 | 88 | while(n2 >= n1) 89 | { 90 | n2 /= n1; p2 ^= p1 * n2; v2 ^= v1 * n2; n2 = hibit(p2); 91 | } 92 | 93 | if(!n2) return v2; 94 | 95 | while(n1 >= n2) 96 | { 97 | n1 /= n2; p1 ^= p2 * n1; v1 ^= v2 * n1; n1 = hibit(p1); 98 | } 99 | } 100 | } 101 | 102 | #endif 103 | 104 | /* The forward and inverse affine transformations used in the S-box */ 105 | 106 | #define fwd_affine(x) \ 107 | (w = (aes_32t)x, w ^= (w<<1)^(w<<2)^(w<<3)^(w<<4), 0x63^(aes_08t)(w^(w>>8))) 108 | 109 | #define inv_affine(x) \ 110 | (w = (aes_32t)x, w = (w<<1)^(w<<3)^(w<<6), 0x05^(aes_08t)(w^(w>>8))) 111 | 112 | static int init = 0; 113 | 114 | void gen_tabs(void) 115 | { aes_32t i, w; 116 | 117 | #if defined(FF_TABLES) 118 | 119 | aes_08t pow[512], log[256]; 120 | 121 | if(init) return; 122 | /* log and power tables for GF(2^8) finite field with 123 | WPOLY as modular polynomial - the simplest primitive 124 | root is 0x03, used here to generate the tables 125 | */ 126 | 127 | i = 0; w = 1; 128 | do 129 | { 130 | pow[i] = (aes_08t)w; 131 | pow[i + 255] = (aes_08t)w; 132 | log[w] = (aes_08t)i++; 133 | w ^= (w << 1) ^ (w & 0x80 ? WPOLY : 0); 134 | } 135 | while (w != 1); 136 | 137 | #else 138 | if(init) return; 139 | #endif 140 | 141 | for(i = 0, w = 1; i < RC_LENGTH; ++i) 142 | { 143 | t_set(r,c)[i] = bytes2word(w, 0, 0, 0); 144 | w = f2(w); 145 | } 146 | 147 | for(i = 0; i < 256; ++i) 148 | { aes_08t b; 149 | 150 | b = fwd_affine(fi((aes_08t)i)); 151 | w = bytes2word(f2(b), b, b, f3(b)); 152 | 153 | #ifdef SBX_SET 154 | t_set(s,box)[i] = b; 155 | #endif 156 | 157 | #ifdef FT1_SET /* tables for a normal encryption round */ 158 | t_set(f,n)[i] = w; 159 | #endif 160 | #ifdef FT4_SET 161 | t_set(f,n)[0][i] = w; 162 | t_set(f,n)[1][i] = upr(w,1); 163 | t_set(f,n)[2][i] = upr(w,2); 164 | t_set(f,n)[3][i] = upr(w,3); 165 | #endif 166 | w = bytes2word(b, 0, 0, 0); 167 | 168 | #ifdef FL1_SET /* tables for last encryption round (may also */ 169 | t_set(f,l)[i] = w; /* be used in the key schedule) */ 170 | #endif 171 | #ifdef FL4_SET 172 | t_set(f,l)[0][i] = w; 173 | t_set(f,l)[1][i] = upr(w,1); 174 | t_set(f,l)[2][i] = upr(w,2); 175 | t_set(f,l)[3][i] = upr(w,3); 176 | #endif 177 | 178 | #ifdef LS1_SET /* table for key schedule if t_set(f,l) above is */ 179 | t_set(l,s)[i] = w; /* not of the required form */ 180 | #endif 181 | #ifdef LS4_SET 182 | t_set(l,s)[0][i] = w; 183 | t_set(l,s)[1][i] = upr(w,1); 184 | t_set(l,s)[2][i] = upr(w,2); 185 | t_set(l,s)[3][i] = upr(w,3); 186 | #endif 187 | 188 | b = fi(inv_affine((aes_08t)i)); 189 | w = bytes2word(fe(b), f9(b), fd(b), fb(b)); 190 | 191 | #ifdef IM1_SET /* tables for the inverse mix column operation */ 192 | t_set(i,m)[b] = w; 193 | #endif 194 | #ifdef IM4_SET 195 | t_set(i,m)[0][b] = w; 196 | t_set(i,m)[1][b] = upr(w,1); 197 | t_set(i,m)[2][b] = upr(w,2); 198 | t_set(i,m)[3][b] = upr(w,3); 199 | #endif 200 | 201 | #ifdef ISB_SET 202 | t_set(i,box)[i] = b; 203 | #endif 204 | #ifdef IT1_SET /* tables for a normal decryption round */ 205 | t_set(i,n)[i] = w; 206 | #endif 207 | #ifdef IT4_SET 208 | t_set(i,n)[0][i] = w; 209 | t_set(i,n)[1][i] = upr(w,1); 210 | t_set(i,n)[2][i] = upr(w,2); 211 | t_set(i,n)[3][i] = upr(w,3); 212 | #endif 213 | w = bytes2word(b, 0, 0, 0); 214 | #ifdef IL1_SET /* tables for last decryption round */ 215 | t_set(i,l)[i] = w; 216 | #endif 217 | #ifdef IL4_SET 218 | t_set(i,l)[0][i] = w; 219 | t_set(i,l)[1][i] = upr(w,1); 220 | t_set(i,l)[2][i] = upr(w,2); 221 | t_set(i,l)[3][i] = upr(w,3); 222 | #endif 223 | } 224 | init = 1; 225 | } 226 | 227 | #endif 228 | 229 | #if defined(__cplusplus) 230 | } 231 | #endif 232 | 233 | -------------------------------------------------------------------------------- /src/bch.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2015 Freescale Semiconductor, Inc. All Rights Reserved. 3 | */ 4 | 5 | /* 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along 17 | * with this program; if not, write to the Free Software Foundation, Inc., 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 19 | */ 20 | /* 21 | * Generic binary BCH encoding/decoding library 22 | * 23 | * This program is free software; you can redistribute it and/or modify it 24 | * under the terms of the GNU General Public License version 2 as published by 25 | * the Free Software Foundation. 26 | * 27 | * This program is distributed in the hope that it will be useful, but WITHOUT 28 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 29 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 30 | * more details. 31 | * 32 | * You should have received a copy of the GNU General Public License along with 33 | * this program; if not, write to the Free Software Foundation, Inc., 51 34 | * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 35 | * 36 | * Copyright © 2011 Parrot S.A. 37 | * 38 | * Author: Ivan Djelic 39 | * 40 | * Description: 41 | * 42 | * This library provides runtime configurable encoding/decoding of binary 43 | * Bose-Chaudhuri-Hocquenghem (BCH) codes. 44 | */ 45 | #ifndef _BCH_H 46 | #define _BCH_H 47 | 48 | //#include 49 | 50 | /** 51 | * struct bch_control - BCH control structure 52 | * @m: Galois field order 53 | * @n: maximum codeword size in bits (= 2^m-1) 54 | * @t: error correction capability in bits 55 | * @ecc_bits: ecc exact size in bits, i.e. generator polynomial degree (<=m*t) 56 | * @ecc_bytes: ecc max size (m*t bits) in bytes 57 | * @a_pow_tab: Galois field GF(2^m) exponentiation lookup table 58 | * @a_log_tab: Galois field GF(2^m) log lookup table 59 | * @mod8_tab: remainder generator polynomial lookup tables 60 | * @ecc_buf: ecc parity words buffer 61 | * @ecc_buf2: ecc parity words buffer 62 | * @xi_tab: GF(2^m) base for solving degree 2 polynomial roots 63 | * @syn: syndrome buffer 64 | * @cache: log-based polynomial representation buffer 65 | * @elp: error locator polynomial 66 | * @poly_2t: temporary polynomials of degree 2t 67 | */ 68 | struct bch_control { 69 | unsigned int m; 70 | unsigned int n; 71 | unsigned int t; 72 | unsigned int ecc_bits; 73 | unsigned int ecc_bytes; 74 | /* private: */ 75 | uint16_t *a_pow_tab; 76 | uint16_t *a_log_tab; 77 | uint32_t *mod8_tab; 78 | uint32_t *ecc_buf; 79 | uint32_t *ecc_buf2; 80 | unsigned int *xi_tab; 81 | unsigned int *syn; 82 | int *cache; 83 | struct gf_poly *elp; 84 | struct gf_poly *poly_2t[4]; 85 | }; 86 | 87 | struct bch_control *init_bch(int m, int t, unsigned int prim_poly); 88 | 89 | void free_bch(struct bch_control *bch); 90 | 91 | void encode_bch(struct bch_control *bch, const uint8_t *data, 92 | unsigned int len, uint8_t *ecc); 93 | 94 | int decode_bch(struct bch_control *bch, const uint8_t *data, unsigned int len, 95 | const uint8_t *recv_ecc, const uint8_t *calc_ecc, 96 | const unsigned int *syn, unsigned int *errloc); 97 | 98 | int encode_bch_ecc(void *source_block, size_t source_size, 99 | void *target_block, size_t target_size, int version); 100 | #endif /* _BCH_H */ 101 | -------------------------------------------------------------------------------- /src/bootstream.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010 Freescale Semiconductor, Inc. All Rights Reserved. 3 | */ 4 | 5 | /* 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along 17 | * with this program; if not, write to the Free Software Foundation, Inc., 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 19 | */ 20 | 21 | #ifndef BOOTSTREAM_H 22 | #define BOOTSTREAM_H 23 | 24 | #include 25 | 26 | //! An AES-128 cipher block is 16 bytes. 27 | typedef uint8_t cipher_block_t[16]; 28 | 29 | //! An AES-128 key is 128 bits, or 16 bytes. 30 | typedef uint8_t aes128_key_t[16]; 31 | 32 | //! A SHA-1 digest is 160 bits, or 20 bytes. 33 | typedef uint8_t sha1_digest_t[20]; 34 | 35 | struct version_t { 36 | uint16_t m_major; 37 | uint16_t m_pad0; 38 | uint16_t m_minor; 39 | uint16_t m_pad1; 40 | uint16_t m_revision; 41 | uint16_t m_pad2; 42 | }; 43 | 44 | struct boot_image_header_t { 45 | sha1_digest_t m_digest; 46 | uint8_t m_signature[4]; 47 | uint8_t m_majorVersion; 48 | uint8_t m_minorVersion; 49 | uint16_t m_flags; 50 | uint32_t m_imageBlocks; 51 | uint32_t m_firstBootTagBlock; 52 | uint32_t m_firstBootableSectionID; 53 | uint16_t m_keyCount; 54 | uint16_t m_keyDictionaryBlock; 55 | uint16_t m_headerBlocks; 56 | uint16_t m_sectionCount; 57 | uint16_t m_sectionHeaderSize; 58 | uint8_t m_padding0[6]; 59 | uint64_t m_timestamp; 60 | struct version_t m_productVersion; 61 | struct version_t m_componentVersion; 62 | uint16_t m_driveTag; 63 | uint8_t m_padding1[6]; 64 | }; 65 | 66 | #define ROM_DISPLAY_PROGRESS (1 << 0) 67 | #define ROM_VERBOSE_PROGRESS (1 << 1) 68 | 69 | struct section_header_t { 70 | uint32_t m_identifier; 71 | uint32_t m_offset; 72 | uint32_t m_length; 73 | uint32_t m_flags; 74 | }; 75 | 76 | #define ROM_SECTION_BOOTABLE (1 << 0) 77 | #define ROM_SECTION_CLEARTEXT (1 << 1) 78 | 79 | struct dek_dictionary_entry_t 80 | { 81 | cipher_block_t m_mac; //!< CBC-MAC of the header. 82 | aes128_key_t m_dek; //!< AES-128 key with which the image payload is encrypted. 83 | }; 84 | 85 | struct boot_command_t { 86 | uint8_t m_checksum; 87 | uint8_t m_tag; 88 | uint16_t m_flags; 89 | uint32_t m_address; 90 | uint32_t m_count; 91 | uint32_t m_data; 92 | }; 93 | #define ROM_NOP_CMD 0x00 94 | #define ROM_TAG_CMD 0x01 95 | #define ROM_LOAD_CMD 0x02 96 | #define ROM_FILL_CMD 0x03 97 | #define ROM_JUMP_CMD 0x04 98 | #define ROM_CALL_CMD 0x05 99 | #define ROM_MODE_CMD 0x06 100 | 101 | static inline uint8_t boot_command_chksum(const struct boot_command_t *bc) 102 | { 103 | const uint8_t *bytes = (const uint8_t *)bc; 104 | uint8_t checksum; 105 | int i; 106 | 107 | checksum = 0x5a; 108 | for (i = 1; i < sizeof(*bc); i++) 109 | checksum += bytes[i]; 110 | 111 | return checksum; 112 | } 113 | 114 | #define BOOTSTREAM_BLOCK_SIZE 16 115 | #define BOOTSTREAM_ALIGN_SIZE(x) (((x) + (BOOTSTREAM_BLOCK_SIZE - 1)) & ~BOOTSTREAM_BLOCK_SIZE) 116 | 117 | int bootstream_verify(int flags, FILE *fp, const uint8_t *key, long *end_of_file); 118 | 119 | char *vec_ascii(const uint8_t *data, char *txt); 120 | char *sha_ascii(const uint8_t *data, char *txt); 121 | uint8_t *ascii_vec(const char *txt, uint8_t *vec); 122 | 123 | #endif 124 | -------------------------------------------------------------------------------- /src/dcp_bootstream_ioctl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010 Freescale Semiconductor, Inc. All Rights Reserved. 3 | */ 4 | 5 | /* 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along 17 | * with this program; if not, write to the Free Software Foundation, Inc., 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 19 | */ 20 | #ifndef DCP_BOOTSTREAM_IOCTL_H 21 | #define DCP_BOOTSTREAM_IOCTL_H 22 | 23 | /* remember to have included the proper _IO definition 24 | * file before hand. 25 | * For user space it's 26 | */ 27 | 28 | #define DBS_IOCTL_BASE 'd' 29 | 30 | typedef uint8_t stmp3xxx_dcp_bootstream_cipher_block[16]; 31 | 32 | #define DBS_ENC _IOW(DBS_IOCTL_BASE, 0x00, stmp3xxx_dcp_bootstream_cipher_block) 33 | #define DBS_DEC _IOW(DBS_IOCTL_BASE, 0x01, stmp3xxx_dcp_bootstream_cipher_block) 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /src/mtd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * mtd.c - Dump control structures of the NAND 3 | * 4 | * Copyright 2008-2016 Freescale Semiconductor, Inc. 5 | * Copyright (c) 2008 by Embedded Alley Solution Inc. 6 | * 7 | * Author: Pantelis Antoniou 8 | * 9 | * This program is free software; you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation; either version 2 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU General Public License 20 | * along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22 | * 23 | */ 24 | 25 | #ifndef MTD_H 26 | #define MTD_H 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | #include "BootControlBlocks.h" 33 | #include "rom_nand_hamming_code_ecc.h" 34 | 35 | //------------------------------------------------------------------------------ 36 | // Re-definitions of true and false, because the standard ones aren't good 37 | // enough? 38 | //------------------------------------------------------------------------------ 39 | 40 | #define false 0 41 | #define true !false 42 | 43 | //------------------------------------------------------------------------------ 44 | // Flag bit definitions for the md->flags member. 45 | //------------------------------------------------------------------------------ 46 | 47 | #define F_VERBOSE 0x01 48 | #define F_MULTICHIP 0x02 49 | #define F_AUTO_MULTICHIP 0x04 50 | 51 | #define vp(x, ...) \ 52 | do { \ 53 | if (((x)->flags) & F_VERBOSE) \ 54 | printf(__VA_ARGS__);\ 55 | } while (0) 56 | 57 | //------------------------------------------------------------------------------ 58 | // This structure contains configuration information. This information comes 59 | // primarily from a .kobs file, but may be overidden from the command line or 60 | // edited as kobs works. 61 | //------------------------------------------------------------------------------ 62 | 63 | struct mtd_config { 64 | int chip_count; 65 | const char *chip_0_device_path; 66 | int chip_0_offset; 67 | int chip_0_size; 68 | const char *chip_1_device_path; 69 | int chip_1_offset; 70 | int chip_1_size; 71 | int search_exponent; 72 | int data_setup_time; 73 | int data_hold_time; 74 | int address_setup_time; 75 | int data_sample_time; 76 | int row_address_size; 77 | int column_address_size; 78 | int read_command_code1; 79 | int read_command_code2; 80 | int boot_stream_major_version; 81 | int boot_stream_minor_version; 82 | int boot_stream_sub_version; 83 | int ncb_version; 84 | int boot_stream_1_address; 85 | int boot_stream_2_address; 86 | 87 | /* for rom boot */ 88 | int stride_size_in_bytes; 89 | int search_area_size_in_bytes; 90 | int search_area_size_in_pages; 91 | /* for i.MX8Q secondary boot container*/ 92 | int secondary_boot_stream_off_in_MB; 93 | }; 94 | 95 | extern const struct mtd_config default_mtd_config; 96 | 97 | //------------------------------------------------------------------------------ 98 | // This structure represents an MTD device in which we will write boot 99 | // information. 100 | //------------------------------------------------------------------------------ 101 | 102 | struct mtd_part { 103 | 104 | // The path to the device node that represents this MTD. 105 | char *name; 106 | 107 | // The file descriptor that represents this MTD. 108 | int fd; 109 | struct mtd_info_user info; 110 | 111 | // A bit set where each bit corresponds to a block in a given MTD. 112 | uint32_t *bad_blocks; 113 | 114 | // The number of bad blocks appearing in this MTD. 115 | int nrbad; 116 | 117 | int oobinfochanged; 118 | int ecc; 119 | }; 120 | 121 | /** 122 | * struct nfc_geometry - NFC geometry description. 123 | * 124 | * This structure describes the NFC's view of the medium geometry. 125 | * 126 | * @ecc_algorithm: The human-readable name of the ECC algorithm 127 | * (e.g., "Reed-Solomon" or "BCH"). 128 | * @ecc_strength: A number that describes the strength of the ECC 129 | * algorithm. 130 | * @page_size_in_bytes: The size, in bytes, of a physical page, including 131 | * both data and OOB. 132 | * @metadata_size_in_bytes: The size, in bytes, of the metadata. 133 | * @ecc_chunk_size_in_bytes: The size, in bytes, of a single ECC chunk. Note 134 | * the first chunk in the page includes both data and 135 | * metadata, so it's a bit larger than this value. 136 | * @ecc_chunk_count: The number of ECC chunks in the page, 137 | * @block_mark_byte_offset: The byte offset in the ECC-based page view at 138 | * which the underlying physical block mark appears. 139 | * @block_mark_bit_offset: The bit offset into the ECC-based page view at 140 | * which the underlying physical block mark appears. 141 | * @ecc_for_meta: Flag shows if uses ecc for meta. 142 | */ 143 | 144 | struct nfc_geometry { 145 | unsigned int gf_len; 146 | unsigned int ecc_strength; 147 | unsigned int page_size_in_bytes; 148 | unsigned int metadata_size_in_bytes; 149 | unsigned int ecc_chunk0_size_in_bytes; 150 | unsigned int ecc_chunkn_size_in_bytes; 151 | unsigned int ecc_chunk_count; 152 | unsigned int payload_size; 153 | unsigned int auxiliary_size; 154 | unsigned int auxiliary_status_offset; 155 | unsigned int block_mark_byte_offset; 156 | unsigned int block_mark_bit_offset; 157 | unsigned int ecc_for_meta; /* separate ECC for meta data */ 158 | 159 | }; 160 | 161 | //------------------------------------------------------------------------------ 162 | // This structure collects information that controls the entire kobs process. 163 | // Various functions contribute information to this structure, depending on the 164 | // command line options and the resulting process flow. 165 | //------------------------------------------------------------------------------ 166 | 167 | struct mtd_data { 168 | struct mtd_part part[2]; 169 | int flags; 170 | struct mtd_config cfg; 171 | 172 | /* writesize + oobsize buffer */ 173 | void *buf; 174 | 175 | /* NFC Geometry */ 176 | struct nfc_geometry nfc_geometry; 177 | 178 | /* NCBs */ 179 | NCB_BootBlockStruct_t *curr_ncb; 180 | NCB_BootBlockStruct_t ncb[2]; 181 | loff_t ncb_ofs[2]; 182 | int ncb_version; /* 0, 1, or 3. Negative means error */ 183 | 184 | /* LDLBs */ 185 | NCB_BootBlockStruct_t *curr_ldlb; 186 | NCB_BootBlockStruct_t ldlb[2]; 187 | loff_t ldlb_ofs[2]; 188 | 189 | /* DBBTs */ 190 | NCB_BootBlockStruct_t *curr_dbbt; 191 | NCB_BootBlockStruct_t dbbt[2]; 192 | loff_t dbbt_ofs[2]; 193 | /* the 2 NANDs */ 194 | BadBlockTableNand_t *bbtn[2]; 195 | 196 | /* In fact, we can reuse the boot block 197 | * struct for mx53 on mx28, it's compatible 198 | */ 199 | 200 | /* i.MX28 FCB */ 201 | BCB_ROM_BootBlockStruct_t fcb; 202 | 203 | /* i.MX28 DBBT */ 204 | BCB_ROM_BootBlockStruct_t dbbt28; 205 | /* i.MX50 DBBT */ 206 | BCB_ROM_BootBlockStruct_t dbbt50; 207 | 208 | /* randomizer flag */ 209 | int randomizer; 210 | 211 | /* raw mode flag */ 212 | int raw_mode_flag; 213 | void *private; 214 | }; 215 | 216 | #define PAGES_PER_STRIDE (mtd_erasesize(md) / mtd_writesize(md)) 217 | #define ROM_MAX_BAD_BLOCKS 425 /* WTF? */ 218 | 219 | //------------------------------------------------------------------------------ 220 | // This variable tells us which version of the ROM we're working with. 221 | //------------------------------------------------------------------------------ 222 | 223 | enum { 224 | ROM_Version_Unknown = -1, 225 | ROM_Version_0 = 0, /* e.g., i.MX23 */ 226 | ROM_Version_1 = 1, /* e.g., i.MX28 */ 227 | ROM_Version_2 = 2, /* e.g., i.MX53 */ 228 | ROM_Version_3 = 3, /* e.g., i.MX50, iMX6dqsl */ 229 | ROM_Version_4 = 4, /* e.g., i.MX6sx */ 230 | ROM_Version_5 = 5, /* e.g., i.MX6sx TO1.2*/ 231 | ROM_Version_6 = 6, /* e.g., i.MX8MQ*/ 232 | ROM_Version_7 = 7, /* e.g., i.MX8Q*/ 233 | }; 234 | 235 | static inline int mtd_pages_per_block(struct mtd_data *md) 236 | { 237 | return md->part[0].info.erasesize / md->part[0].info.writesize; 238 | } 239 | 240 | static inline unsigned int mtd_bytes2blocks(struct mtd_data *md, unsigned int bytes) 241 | { 242 | return bytes / md->part[0].info.erasesize; 243 | } 244 | 245 | static inline unsigned int mtd_bytes2pages(struct mtd_data *md, unsigned int bytes) 246 | { 247 | return bytes / md->part[0].info.writesize; 248 | } 249 | 250 | static inline unsigned int mtd_size(struct mtd_data *md) 251 | { 252 | return md->part[0].info.size; 253 | } 254 | 255 | static inline unsigned int mtd_erasesize(struct mtd_data *md) 256 | { 257 | return md->part[0].info.erasesize; 258 | } 259 | 260 | static inline unsigned int mtd_writesize(struct mtd_data *md) 261 | { 262 | return md->part[0].info.writesize; 263 | } 264 | 265 | static inline unsigned int mtd_oobsize(struct mtd_data *md) 266 | { 267 | return md->part[0].info.oobsize; 268 | } 269 | 270 | static inline int mtd_nrbad(struct mtd_data *md, int chip) 271 | { 272 | return md->part[chip].nrbad; 273 | } 274 | 275 | int mtd_isbad(struct mtd_data *md, int chip, loff_t ofs); 276 | int mtd_erase(struct mtd_data *md, int chip, loff_t ofs, size_t size); 277 | int mtd_erase_block(struct mtd_data *md, int chip, loff_t ofs); 278 | int mtd_read(struct mtd_data *md, int chip, void *data, size_t size, loff_t ofs); 279 | int mtd_read_page(struct mtd_data *md, int chip, loff_t ofs, int ecc); 280 | int mtd_write(struct mtd_data *md, int chip, const void *data, size_t size, loff_t ofs); 281 | int mtd_write_page(struct mtd_data *md, int chip, loff_t ofs, int ecc); 282 | void mtd_dump(struct mtd_data *md); 283 | struct mtd_data *mtd_open(const struct mtd_config *cfg, int flags); 284 | void mtd_close(struct mtd_data *md); 285 | void *mtd_load_boot_structure(struct mtd_data *md, int chip, loff_t *ofsp, loff_t end, 286 | uint32_t magic1, uint32_t magic2, uint32_t magic3, int use_ecc, 287 | int magic_offset); 288 | int mtd_load_all_boot_structures(struct mtd_data *md); 289 | int mtd_dump_structure(struct mtd_data *md); 290 | 291 | int v0_rom_mtd_init(struct mtd_data *md, FILE *fp); 292 | int v1_rom_mtd_init(struct mtd_data *md, FILE *fp); 293 | int v2_rom_mtd_init(struct mtd_data *md, FILE *fp); 294 | int v3_rom_mtd_init(struct mtd_data *md, FILE *fp); 295 | int v4_rom_mtd_init(struct mtd_data *md, FILE *fp); 296 | 297 | int mtd_markbad(struct mtd_data *md, int chip, loff_t ofs); 298 | 299 | #define UPDATE_NCB 0x01 300 | #define UPDATE_LDLB 0x02 301 | #define UPDATE_DBBT 0x04 302 | #define UPDATE_BS0 0x08 303 | #define UPDATE_BS1 0x10 304 | #define UPDATE_BS(x) (0x08 << ((x) & 1)) 305 | #define UPDATE_ALL (UPDATE_NCB | UPDATE_LDLB | UPDATE_DBBT | UPDATE_BS0 | UPDATE_BS1) 306 | 307 | int v0_rom_mtd_commit_structures(struct mtd_data *md, FILE *fp, int flags); 308 | int v1_rom_mtd_commit_structures(struct mtd_data *md, FILE *fp, int flags); 309 | int v2_rom_mtd_commit_structures(struct mtd_data *md, FILE *fp, int flags); 310 | int v3_rom_mtd_commit_structures(struct mtd_data *md, FILE *fp, int flags); 311 | int v4_rom_mtd_commit_structures(struct mtd_data *md, FILE *fp, int flags); 312 | int v5_rom_mtd_commit_structures(struct mtd_data *md, FILE *fp, int flags); 313 | int v6_rom_mtd_commit_structures(struct mtd_data *md, FILE *fp, int flags); 314 | int v7_rom_mtd_commit_structures(struct mtd_data *md, FILE *fp, int flags); 315 | 316 | int mtd_set_ecc_mode(struct mtd_data *md, int ecc); 317 | 318 | #ifndef ARRAY_SIZE 319 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) 320 | #endif 321 | 322 | void mtd_parse_args(struct mtd_config *cfg, int argc, char **argv); 323 | void mtd_parse_kobs(struct mtd_config *cfg, const char *name, int verbose); 324 | void mtd_cfg_dump(struct mtd_config *cfg); 325 | 326 | int ncb_get_version(void *ncb_candidate, NCB_BootBlockStruct_t **result); 327 | int ncb_encrypt(NCB_BootBlockStruct_t *ncb, void *target, size_t size, int version); 328 | int fcb_encrypt(BCB_ROM_BootBlockStruct_t *fcb, void *target, size_t size, int version); 329 | 330 | void copy_bits(uint8_t *dst, size_t dst_bit_off, 331 | uint8_t *src, size_t src_bit_off, 332 | size_t nbits); 333 | void swap_bad_block_mark(void *data, void *oob, 334 | struct nfc_geometry* nfc_geo, int wr_flag); 335 | #endif 336 | -------------------------------------------------------------------------------- /src/ncb.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ncb.c - verify and encode NCB 3 | * 4 | * Copyright (C) 2014-2015 Freescale Semiconductor, Inc. 5 | * Copyright (c) 2008 by Embedded Alley Solution Inc. 6 | * 7 | * This program is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 | * 21 | */ 22 | #define _GNU_SOURCE 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include "mtd.h" 31 | #include "config.h" 32 | #include "rom_nand_hamming_code_ecc.h" 33 | #include "bch.h" 34 | 35 | #define MAX_HAMMING_FCB_SZ 220 36 | 37 | static inline int even_number_of_1s(uint8_t byte) 38 | { 39 | int even = 1; 40 | 41 | while (byte > 0) { 42 | even ^= (byte & 0x1); 43 | byte >>= 1; 44 | } 45 | return even; 46 | } 47 | 48 | 49 | #define BIT(v,n) (((v) >> (n)) & 0x1) 50 | #define B(n) (BIT(d,n)) 51 | #define BSEQ(a1,a2,a3,a4,a5,a6,a7,a8) \ 52 | (B(a1) ^ B(a2) ^ B(a3) ^ B(a4) ^ B(a5) ^ B(a6) ^ B(a7) ^ B(a8)) 53 | 54 | static uint8_t calculate_parity_22_16(uint16_t d) 55 | { 56 | uint8_t p = 0; 57 | 58 | if (d == 0 || d == 0xFFFF) 59 | return 0; /* optimization :) */ 60 | 61 | p |= BSEQ(15, 12, 11, 8, 5, 4, 3, 2) << 0; 62 | p |= BSEQ(13, 12, 11, 10, 9, 7, 3, 1) << 1; 63 | p |= BSEQ(15, 14, 13, 11, 10, 9, 6, 5) << 2; 64 | p |= BSEQ(15, 14, 13, 8, 7, 6, 4, 0) << 3; 65 | p |= BSEQ(12, 9, 8, 7, 6, 2, 1, 0) << 4; 66 | p |= BSEQ(14, 10, 5, 4, 3, 2, 1, 0) << 5; 67 | return p; 68 | } 69 | 70 | static uint8_t calculate_parity_13_8(uint8_t d) 71 | { 72 | uint8_t p = 0; 73 | 74 | p |= (B(6) ^ B(5) ^ B(3) ^ B(2)) << 0; 75 | p |= (B(7) ^ B(5) ^ B(4) ^ B(2) ^ B(1)) << 1; 76 | p |= (B(7) ^ B(6) ^ B(5) ^ B(1) ^ B(0)) << 2; 77 | p |= (B(7) ^ B(4) ^ B(3) ^ B(0)) << 3; 78 | p |= (B(6) ^ B(4) ^ B(3) ^ B(2) ^ B(1) ^ B(0)) << 4; 79 | return p; 80 | } 81 | #undef BIT 82 | #undef B 83 | #undef BSEQ 84 | 85 | static int encode_hamming_code_22_16(void *source_block, size_t source_size, 86 | void *target_block, size_t target_size) 87 | { 88 | int i, j, bit_index; 89 | uint16_t *src; 90 | uint8_t *dst; 91 | uint8_t np; 92 | uint8_t ecc[NAND_HC_ECC_SIZEOF_PARITY_BLOCK_IN_BYTES]; 93 | uint8_t data[NAND_HC_ECC_SIZEOF_DATA_BLOCK_IN_BYTES]; 94 | 95 | memset(data, 0, ARRAY_SIZE(data)); 96 | memcpy(data, source_block, source_size); 97 | 98 | src = (uint16_t *) data; 99 | dst = (uint8_t *) target_block; 100 | 101 | /* create THREE copies of source block */ 102 | for (i = 0; i < NAND_HC_ECC_SIZEOF_DATA_BLOCK_IN_BYTES; i++) { 103 | dst[i + NAND_HC_ECC_OFFSET_FIRST_DATA_COPY] = 104 | dst[i + NAND_HC_ECC_OFFSET_SECOND_DATA_COPY] = 105 | dst[i + NAND_HC_ECC_OFFSET_THIRD_DATA_COPY] = 106 | ((uint8_t *) src)[i]; 107 | } 108 | 109 | /* finally, point to the end of populated data */ 110 | for (bit_index = j = i = 0; 111 | j < NAND_HC_ECC_SIZEOF_DATA_BLOCK_IN_BYTES / sizeof(uint16_t); 112 | j++) { 113 | np = calculate_parity_22_16(src[j]); 114 | 115 | switch (bit_index) { 116 | 117 | case 0: 118 | ecc[i] = np & 0x3F; 119 | break; 120 | case 2: 121 | ecc[i++] |= (np & 0x03) << 6; 122 | ecc[i] = (np & 0x3C) >> 2; 123 | break; 124 | case 4: 125 | ecc[i++] |= (np & 0x0F) << 4; 126 | ecc[i] = (np & 0x30) >> 4; 127 | break; 128 | case 6: 129 | ecc[i++] |= (np & 0x3F) << 2; 130 | break; 131 | } 132 | bit_index = (bit_index + 2) % 8; 133 | } 134 | 135 | for (i = 0; i < NAND_HC_ECC_SIZEOF_PARITY_BLOCK_IN_BYTES; i++) { 136 | dst[i + NAND_HC_ECC_OFFSET_FIRST_PARITY_COPY] = 137 | dst[i + NAND_HC_ECC_OFFSET_SECOND_PARITY_COPY] = 138 | dst[i + NAND_HC_ECC_OFFSET_THIRD_PARITY_COPY] = ecc[i]; 139 | } 140 | 141 | return 0; 142 | } 143 | 144 | 145 | static int encode_hamming_code_13_8(void *source_block, size_t source_size, 146 | void *target_block, size_t target_size) 147 | { 148 | uint8_t ecc[NAND_HC_ECC_SIZEOF_DATA_BLOCK_IN_BYTES]; 149 | uint8_t data[NAND_HC_ECC_SIZEOF_DATA_BLOCK_IN_BYTES]; 150 | int i; 151 | 152 | memset(ecc, 0, ARRAY_SIZE(ecc)); 153 | memset(data, 0, ARRAY_SIZE(data)); 154 | memcpy(data, source_block, source_size); 155 | 156 | for (i = 0; i < source_size; i ++) 157 | ecc[i] = calculate_parity_13_8(data[i]); 158 | 159 | memcpy((uint8_t*)target_block + BCB_MAGIC_OFFSET, data, NAND_HC_ECC_SIZEOF_DATA_BLOCK_IN_BYTES); 160 | memcpy((uint8_t*)target_block + BCB_MAGIC_OFFSET + NAND_HC_ECC_SIZEOF_DATA_BLOCK_IN_BYTES, 161 | ecc, NAND_HC_ECC_SIZEOF_DATA_BLOCK_IN_BYTES); 162 | 163 | return 0; 164 | } 165 | 166 | static int ncb_single_pair_check(uint8_t *n1, uint8_t *p1, uint8_t *n2, uint8_t *p2) 167 | { 168 | return (memcmp(n1, n2, NAND_HC_ECC_SIZEOF_DATA_BLOCK_IN_BYTES) == 0 && 169 | memcmp(p1, p2, NAND_HC_ECC_SIZEOF_PARITY_BLOCK_IN_BYTES) == 0); 170 | } 171 | 172 | static int ncb_triple_check(void *page) 173 | { 174 | uint8_t *n1 = (uint8_t*)page + NAND_HC_ECC_OFFSET_FIRST_DATA_COPY, 175 | *n2 = (uint8_t*)page + NAND_HC_ECC_OFFSET_SECOND_DATA_COPY, 176 | *n3 = (uint8_t*)page + NAND_HC_ECC_OFFSET_THIRD_DATA_COPY, 177 | *p1 = (uint8_t*)page + NAND_HC_ECC_OFFSET_FIRST_PARITY_COPY, 178 | *p2 = (uint8_t*)page + NAND_HC_ECC_OFFSET_SECOND_PARITY_COPY, 179 | *p3 = (uint8_t*)page + NAND_HC_ECC_OFFSET_THIRD_PARITY_COPY; 180 | 181 | if (ncb_single_pair_check(n1, p1, n2, p2)) 182 | return 1; 183 | if (ncb_single_pair_check(n2, p2, n3, p3)) 184 | return 2; 185 | if (ncb_single_pair_check(n1, p1, n3, p3)) 186 | return 1; 187 | return -1; 188 | } 189 | 190 | static int lookup_single_error_22_16(uint8_t syndrome) 191 | { 192 | int i; 193 | uint8_t syndrome_table[] = { 194 | 0x38, 0x32, 0x31, 0x23, 0x29, 0x25, 0x1C, 0x1A, 195 | 0x19, 0x16, 0x26, 0x07, 0x13, 0x0E, 0x2C, 0x0D, 196 | 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 197 | }; 198 | 199 | for (i = 0; i < ARRAY_SIZE(syndrome_table); i ++) 200 | if (syndrome_table[i] == syndrome) 201 | return i; 202 | return -ENOENT; 203 | } 204 | 205 | static int lookup_single_error_13_8(uint8_t syndrome) 206 | { 207 | int i; 208 | uint8_t syndrome_table[] = { 209 | 0x1C, 0x16, 0x13, 0x19, 210 | 0x1A, 0x07, 0x15, 0x0E, 211 | 0x01, 0x02, 0x04, 0x08, 212 | 0x10, 213 | }; 214 | 215 | for (i = 0; i < ARRAY_SIZE(syndrome_table); i ++) 216 | if (syndrome_table[i] == syndrome) 217 | return i; 218 | return -ENOENT; 219 | } 220 | 221 | static inline NCB_BootBlockStruct_t *ncb_verify_hamming_22_16(void *page) 222 | { 223 | int r; 224 | uint16_t* n1 = (uint16_t*)((uint8_t*)page + NAND_HC_ECC_OFFSET_FIRST_DATA_COPY), 225 | *n2 = ((uint16_t*)(uint8_t*)page + NAND_HC_ECC_OFFSET_SECOND_DATA_COPY), 226 | *n, *data; 227 | uint8_t *p1 = (uint8_t*)page + NAND_HC_ECC_OFFSET_FIRST_PARITY_COPY, 228 | *p2 = (uint8_t*)page + NAND_HC_ECC_OFFSET_SECOND_PARITY_COPY, 229 | *parity, p, np, syndrome; 230 | int bit_index, i, j, bit_to_flip; 231 | 232 | r = ncb_triple_check(page); 233 | if (r < 0) 234 | return NULL; 235 | if (r == 1) { 236 | data = n = n1; 237 | parity = p1; 238 | } 239 | else if (r == 2) { 240 | data = n = n2; 241 | parity = p2; 242 | } 243 | else { 244 | fprintf(stderr, "internal error: %s, r = %d\n", __func__, r); 245 | return NULL; 246 | } 247 | 248 | for (bit_index = i = j = 0, r = 0; 249 | i < NAND_HC_ECC_SIZEOF_DATA_BLOCK_IN_BYTES / sizeof(uint16_t) && r == 0; 250 | i ++, data++) { 251 | 252 | switch (bit_index) { 253 | 254 | case 0: 255 | p = parity[j] & 0x3F; 256 | break; 257 | case 2: 258 | p = (parity[j++] & 0xC0) >> 6; 259 | p |= (parity[j] & 0x0F) << 2; 260 | break; 261 | case 4: 262 | p = (parity[j++] & 0xF0) >> 4; 263 | p |= (parity[j] & 0x03) << 4; 264 | break; 265 | case 6: 266 | p = (parity[j++] & 0xFC) >> 2; 267 | break; 268 | default: 269 | fprintf(stderr, "internal error at %s:%d\n", __func__, __LINE__); 270 | exit(5); 271 | } 272 | bit_index = (bit_index + 2) % 8; 273 | 274 | np = calculate_parity_22_16(*data); 275 | syndrome = np ^ p; 276 | if (syndrome == 0) /* cool */ { 277 | continue; 278 | } 279 | 280 | if (even_number_of_1s(syndrome)) { 281 | r = i; 282 | break; 283 | } 284 | 285 | bit_to_flip = lookup_single_error_22_16(syndrome); 286 | if (bit_to_flip < 0) { 287 | r = i; 288 | break; 289 | } 290 | 291 | if (bit_to_flip < 16) 292 | *data ^= (1 << bit_to_flip); 293 | } 294 | return r == 0 ? (NCB_BootBlockStruct_t*)n : NULL; 295 | } 296 | 297 | static inline NCB_BootBlockStruct_t *ncb_verify_hamming_13_8(void *ncb_page) 298 | { 299 | int i, bit_to_flip; 300 | uint8_t *data, *parity, np, syndrome; 301 | 302 | data = (uint8_t*)ncb_page + 12; 303 | parity = (uint8_t*)ncb_page + 12 + NAND_HC_ECC_SIZEOF_DATA_BLOCK_IN_BYTES; 304 | 305 | for (i = 0; i < NAND_HC_ECC_SIZEOF_DATA_BLOCK_IN_BYTES; i ++, data++) { 306 | np = calculate_parity_13_8(*data); 307 | syndrome = np ^ parity[i]; 308 | if (syndrome == 0) 309 | continue; 310 | 311 | if (even_number_of_1s(syndrome)) 312 | return NULL; 313 | 314 | bit_to_flip = lookup_single_error_13_8(syndrome); 315 | if (bit_to_flip < 0) 316 | return NULL; 317 | 318 | if (bit_to_flip < 8) 319 | *data ^= (1 << bit_to_flip); 320 | } 321 | return (NCB_BootBlockStruct_t*)(ncb_page + 12); 322 | } 323 | 324 | /** 325 | * ncb_encrypt - Encrypt the NCB block, assuming that target system uses NCB 326 | * version 'version' 327 | * 328 | * ncb: Points to valid BootBlockStruct_t structure 329 | * target: Points to a buffer large enough to contain an entire NAND Flash page 330 | * (both data and OOB). 331 | * size: The size of an entire NAND Flash page (both data and OOB). 332 | * version: The version number of the NCB. 333 | */ 334 | int ncb_encrypt(NCB_BootBlockStruct_t *ncb, void *target, size_t size, int version) 335 | { 336 | assert(version == 0 || version == 1 || version == 3); 337 | assert(size >= sizeof(NCB_BootBlockStruct_t)); 338 | 339 | memset(target, 0, size); 340 | 341 | switch (version) 342 | { 343 | case 0: 344 | memcpy(target, ncb, sizeof(*ncb)); 345 | return size; 346 | case 1: 347 | return encode_hamming_code_22_16(ncb, sizeof(*ncb), target, size); 348 | case 3: 349 | return encode_hamming_code_13_8(ncb, sizeof(*ncb), target, size); 350 | default: 351 | fprintf(stderr, "NCB version == %d? Something is wrong!\n", version); 352 | return -EINVAL; 353 | } 354 | } 355 | 356 | /** 357 | * fcb_encrypt - Encrypt the FCB block, assuming that target system uses NCB 358 | * version 'version' 359 | * 360 | * fcb: Points to valid imx28_BootBlockStruct_t structure. 361 | * target: Points to a buffer large enough to contain an entire NAND Flash page 362 | * (both data and OOB). 363 | * size: The size of an entire NAND Flash page (both data and OOB). 364 | * version: The version number of the NCB. 365 | * 366 | */ 367 | int fcb_encrypt(BCB_ROM_BootBlockStruct_t *fcb, void *target, size_t size, int version) 368 | { 369 | uint32_t accumulator; 370 | uint8_t *p; 371 | uint8_t *q; 372 | int fcb_size; 373 | 374 | //---------------------------------------------------------------------- 375 | // Check for nonsense. 376 | //---------------------------------------------------------------------- 377 | 378 | assert(size >= sizeof(BCB_ROM_BootBlockStruct_t)); 379 | 380 | //---------------------------------------------------------------------- 381 | // Clear out the target. 382 | //---------------------------------------------------------------------- 383 | 384 | memset(target, 0, size); 385 | 386 | //---------------------------------------------------------------------- 387 | // Compute the checksum. 388 | // 389 | // Note that we're computing the checksum only over the FCB itself, 390 | // whereas it's actually supposed to reflect the entire 508 bytes 391 | // in the FCB page between the base of the of FCB and the base of the 392 | // ECC bytes. However, the entire space between the top of the FCB and 393 | // the base of the ECC bytes will be all zeros, so this is OK. 394 | //---------------------------------------------------------------------- 395 | 396 | p = ((uint8_t *) fcb) + 4; 397 | q = (uint8_t *) (fcb + 1); 398 | 399 | accumulator = 0; 400 | 401 | for (; p < q; p++) { 402 | accumulator += *p; 403 | } 404 | 405 | accumulator ^= 0xffffffff; 406 | 407 | fcb->m_u32Checksum = accumulator; 408 | 409 | fcb_size = MAX_HAMMING_FCB_SZ < sizeof(*fcb) ? MAX_HAMMING_FCB_SZ : sizeof(*fcb); 410 | 411 | //---------------------------------------------------------------------- 412 | // Compute the ECC bytes. 413 | //---------------------------------------------------------------------- 414 | 415 | switch (version) 416 | { 417 | case 0: 418 | memcpy(target, fcb, sizeof(*fcb)); 419 | return size; 420 | case 1: 421 | return encode_hamming_code_13_8(fcb, fcb_size, target, size); 422 | case 2: 423 | return encode_bch_ecc(fcb, sizeof(*fcb), target, size, version); 424 | case 3: 425 | return encode_bch_ecc(fcb, sizeof(*fcb), target, size, version); 426 | default: 427 | fprintf(stderr, "FCB version == %d? Something is wrong!\n", version); 428 | return -EINVAL; 429 | } 430 | } 431 | 432 | /** 433 | * ncb_get_version - parse the boot block ncb_candidate and on success store 434 | * the pointer to NCB to result 435 | * 436 | * returns version of found NCB, or -1 otherwise - try further scanning.. 437 | */ 438 | int ncb_get_version(void *ncb_candidate, NCB_BootBlockStruct_t **result) 439 | { 440 | NCB_BootBlockStruct_t *bbs, *p1, *p2, *p3; 441 | 442 | assert(result != NULL); 443 | 444 | *result = NULL; 445 | 446 | /* first of all, check version 3 of ncb */ 447 | bbs = (NCB_BootBlockStruct_t*)((uint8_t*)ncb_candidate + 12); 448 | if (bbs->m_u32FingerPrint1 == NCB_FINGERPRINT1 && 449 | bbs->m_u32FingerPrint2 == NCB_FINGERPRINT2 && 450 | bbs->m_u32FingerPrint3 == NCB_FINGERPRINT3) { 451 | /* fingerprints match, so it is either v3 or corrupted NCB */ 452 | *result = ncb_verify_hamming_13_8(ncb_candidate); 453 | if (*result) 454 | return 3; 455 | fprintf(stderr, "ncb_verify_hamming_13_8 failed!\n"); 456 | return -1; 457 | } 458 | 459 | /* 460 | * then, check if it is version 1 (yes, there was not NCBv2 461 | * 462 | * To match the v1, there should be at least two identical copies 463 | * of NCB 464 | */ 465 | 466 | p1 = (NCB_BootBlockStruct_t*)ncb_candidate /* + NAND_HC_OFFSET_FIRST_DATA_COPY */; 467 | p2 = (NCB_BootBlockStruct_t*)((uint8_t*)ncb_candidate + NAND_HC_ECC_OFFSET_SECOND_DATA_COPY); 468 | p3 = (NCB_BootBlockStruct_t*)((uint8_t*)ncb_candidate + NAND_HC_ECC_OFFSET_THIRD_DATA_COPY); 469 | 470 | if (memcmp(p1, p2, sizeof(NCB_BootBlockStruct_t)) == 0 || 471 | memcmp(p1, p3, sizeof(NCB_BootBlockStruct_t)) == 0 || 472 | memcmp(p2, p3, sizeof(NCB_BootBlockStruct_t)) == 0) { 473 | /* 474 | * we found at least two identical copies of NCB; verify 475 | * against the ECC 476 | */ 477 | *result = ncb_verify_hamming_22_16(ncb_candidate); 478 | if (*result) 479 | return 1; 480 | return -1; 481 | } 482 | 483 | /* 484 | * and finally, does it look like NCBv0 ? 485 | */ 486 | if (p1->m_u32FingerPrint1 == NCB_FINGERPRINT1 && 487 | p1->m_u32FingerPrint2 == NCB_FINGERPRINT2 && 488 | p1->m_u32FingerPrint3 == NCB_FINGERPRINT3) { 489 | *result = p1; 490 | return 0; 491 | } 492 | 493 | /* 494 | * we did try. 495 | */ 496 | return -1; 497 | } 498 | 499 | 500 | -------------------------------------------------------------------------------- /src/plat_boot_config.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2016 Freescale Semiconductor, Inc. All Rights Reserved. 3 | */ 4 | 5 | /* 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along 17 | * with this program; if not, write to the Free Software Foundation, Inc., 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | #include "mtd.h" 26 | #include "plat_boot_config.h" 27 | 28 | /* System version */ 29 | platform_config *plat_config_data; 30 | 31 | static platform_config mx23_boot_config = { 32 | .m_u32RomVer = ROM_Version_0, 33 | .m_u32EnDISBBM = 0, 34 | .m_u32EnSoftEcc = 0, 35 | .m_u32EnBootStreamVerify = 1, 36 | .m_u32UseNfcGeo = 1, 37 | .m_u32UseMultiBootArea = 1, 38 | .m_u32UseSinglePageStride = 0, 39 | .m_u32DBBT_FingerPrint = DBBT_FINGERPRINT2, 40 | .m_u32MaxEccStrength = 20, 41 | .rom_mtd_init = v0_rom_mtd_init, 42 | .rom_mtd_commit_structures = v0_rom_mtd_commit_structures, 43 | }; 44 | 45 | static platform_config mx28_boot_config = { 46 | .m_u32RomVer = ROM_Version_1, 47 | .m_u32EnDISBBM = 0, 48 | .m_u32EnSoftEcc = 1, 49 | .m_u32EnBootStreamVerify = 1, 50 | .m_u32UseNfcGeo = 1, 51 | .m_u32UseMultiBootArea = 0, 52 | .m_u32UseSinglePageStride = 1, 53 | .m_u32DBBT_FingerPrint = DBBT_FINGERPRINT2, 54 | .m_u32MaxEccStrength = 20, 55 | .rom_mtd_init = v1_rom_mtd_init, 56 | .rom_mtd_commit_structures = v1_rom_mtd_commit_structures, 57 | }; 58 | 59 | static platform_config mx53to1_boot_config = { 60 | .m_u32RomVer = ROM_Version_2, 61 | .m_u32EnDISBBM = 0, 62 | .m_u32EnSoftEcc = 0, 63 | .m_u32EnBootStreamVerify = 0, 64 | .m_u32UseNfcGeo = 0, 65 | .m_u32UseMultiBootArea = 0, 66 | .m_u32UseSinglePageStride = 0, 67 | .m_u32DBBT_FingerPrint = DBBT_FINGERPRINT2_V2, 68 | .m_u32MaxEccStrength = 40, 69 | .rom_mtd_init = v2_rom_mtd_init, 70 | .rom_mtd_commit_structures = v2_rom_mtd_commit_structures, 71 | }; 72 | 73 | static platform_config mx53to2_boot_config = { 74 | .m_u32RomVer = ROM_Version_2, 75 | .m_u32EnDISBBM = 1, 76 | .m_u32EnSoftEcc = 0, 77 | .m_u32EnBootStreamVerify = 0, 78 | .m_u32UseNfcGeo = 0, 79 | .m_u32UseMultiBootArea = 0, 80 | .m_u32UseSinglePageStride = 0, 81 | .m_u32DBBT_FingerPrint = DBBT_FINGERPRINT2_V2, 82 | .m_u32MaxEccStrength = 40, 83 | .rom_mtd_init = v2_rom_mtd_init, 84 | .rom_mtd_commit_structures = v2_rom_mtd_commit_structures, 85 | }; 86 | 87 | static platform_config mx50_boot_config = { 88 | .m_u32RomVer = ROM_Version_3, 89 | .m_u32EnDISBBM = 0, 90 | .m_u32EnSoftEcc = 1, 91 | .m_u32EnBootStreamVerify = 0, 92 | .m_u32UseNfcGeo = 1, 93 | .m_u32UseMultiBootArea = 0, 94 | .m_u32UseSinglePageStride = 0, 95 | .m_u32DBBT_FingerPrint = DBBT_FINGERPRINT2, 96 | .m_u32MaxEccStrength = 40, 97 | .rom_mtd_init = v4_rom_mtd_init, 98 | .rom_mtd_commit_structures = v4_rom_mtd_commit_structures, 99 | }; 100 | 101 | static platform_config mx6q_boot_config = { 102 | .m_u32RomVer = ROM_Version_3, 103 | .m_u32EnDISBBM = 0, 104 | .m_u32EnBootStreamVerify = 0, 105 | .m_u32UseNfcGeo = 0, 106 | .m_u32UseMultiBootArea = 0, 107 | .m_u32UseSinglePageStride = 0, 108 | .m_u32DBBT_FingerPrint = DBBT_FINGERPRINT2, 109 | .m_u32MaxEccStrength = 40, 110 | .rom_mtd_init = v4_rom_mtd_init, 111 | .rom_mtd_commit_structures = v4_rom_mtd_commit_structures, 112 | }; 113 | 114 | static platform_config mx6sx_boot_config = { 115 | .m_u32RomVer = ROM_Version_4, 116 | .m_u32EnDISBBM = 0, 117 | .m_u32EnBootStreamVerify = 0, 118 | .m_u32UseNfcGeo = 0, 119 | .m_u32UseMultiBootArea = 0, 120 | .m_u32UseSinglePageStride = 0, 121 | .m_u32DBBT_FingerPrint = DBBT_FINGERPRINT2, 122 | .m_u32MaxEccStrength = 62, 123 | .rom_mtd_init = v4_rom_mtd_init, 124 | .rom_mtd_commit_structures = v4_rom_mtd_commit_structures, 125 | }; 126 | 127 | static platform_config mx6sx_to_1_2_boot_config = { 128 | .m_u32RomVer = ROM_Version_5, 129 | .m_u32EnDISBBM = 0, 130 | .m_u32EnBootStreamVerify = 0, 131 | .m_u32UseNfcGeo = 0, 132 | .m_u32UseMultiBootArea = 0, 133 | .m_u32UseSinglePageStride = 0, 134 | .m_u32DBBT_FingerPrint = DBBT_FINGERPRINT2, 135 | .m_u32MaxEccStrength = 62, 136 | .rom_mtd_init = v4_rom_mtd_init, 137 | .rom_mtd_commit_structures = v5_rom_mtd_commit_structures, 138 | }; 139 | 140 | static platform_config mx7d_boot_config = { 141 | .m_u32RomVer = ROM_Version_5, 142 | .m_u32EnDISBBM = 0, 143 | .m_u32EnBootStreamVerify = 0, 144 | .m_u32UseNfcGeo = 0, 145 | .m_u32UseMultiBootArea = 0, 146 | .m_u32UseSinglePageStride = 0, 147 | .m_u32DBBT_FingerPrint = DBBT_FINGERPRINT2, 148 | .m_u32MaxEccStrength = 62, 149 | .rom_mtd_init = v4_rom_mtd_init, 150 | .rom_mtd_commit_structures = v5_rom_mtd_commit_structures, 151 | }; 152 | 153 | static platform_config mx6ul_boot_config = { 154 | .m_u32RomVer = ROM_Version_5, 155 | .m_u32EnDISBBM = 0, 156 | .m_u32EnBootStreamVerify = 0, 157 | .m_u32UseNfcGeo = 0, 158 | .m_u32UseMultiBootArea = 0, 159 | .m_u32UseSinglePageStride = 0, 160 | .m_u32DBBT_FingerPrint = DBBT_FINGERPRINT2, 161 | .m_u32MaxEccStrength = 40, 162 | .rom_mtd_init = v4_rom_mtd_init, 163 | .rom_mtd_commit_structures = v6_rom_mtd_commit_structures, 164 | }; 165 | 166 | static platform_config mx8mq_boot_config = { 167 | .m_u32RomVer = ROM_Version_6, 168 | .m_u32EnDISBBM = 0, 169 | .m_u32EnBootStreamVerify = 0, 170 | .m_u32UseNfcGeo = 0, 171 | .m_u32UseMultiBootArea = 0, 172 | .m_u32UseSinglePageStride = 0, 173 | .m_u32DBBT_FingerPrint = DBBT_FINGERPRINT2, 174 | .m_u32MaxEccStrength = 62, 175 | .rom_mtd_init = v4_rom_mtd_init, 176 | .rom_mtd_commit_structures = v7_rom_mtd_commit_structures, 177 | }; 178 | 179 | static platform_config mx8q_boot_config = { 180 | .m_u32RomVer = ROM_Version_7, 181 | .m_u32EnDISBBM = 0, 182 | .m_u32EnBootStreamVerify = 0, 183 | .m_u32UseNfcGeo = 0, 184 | .m_u32UseMultiBootArea = 0, 185 | .m_u32UseSinglePageStride = 0, 186 | .m_u32DBBT_FingerPrint = DBBT_FINGERPRINT2, 187 | .m_u32MaxEccStrength = 62, 188 | .rom_mtd_init = v4_rom_mtd_init, 189 | .rom_mtd_commit_structures = v5_rom_mtd_commit_structures, 190 | }; 191 | 192 | int discover_boot_rom_version(void) 193 | { 194 | FILE *cpuinfo; 195 | FILE *soc_id; 196 | FILE *revision; 197 | char line_buffer[100]; 198 | static char *banner = "Revision"; 199 | static char *banner_hw = "Hardware"; 200 | static char *plat_imx6sx = "i.MX6SX"; 201 | static char *plat_imx6ul = "i.MX6UL"; 202 | static char *plat_imx6ull = "i.MX6ULL"; 203 | static char *plat_imx8q = "i.MX8Q"; /* i.MX8QM or i.MX8QXP */ 204 | static char *plat_imx8dxl = "i.MX8DXL"; /* i.MX8DXL */ 205 | static char *plat_imx8mq = "i.MX8MQ"; /* i.MX8MQ(mscale) */ 206 | static char *plat_imx8mm = "i.MX8MM"; 207 | static char *plat_imx8mn = "i.MX8MN"; 208 | static char *plat_imx8mp = "i.MX8MP"; 209 | char *rev; 210 | int system_rev, hw_system_rev = 0; 211 | 212 | 213 | /* check if it is i.MX8 platform */ 214 | soc_id = fopen("/sys/devices/soc0/soc_id", "r"); 215 | 216 | if (soc_id) { 217 | fgets(line_buffer, sizeof(line_buffer), soc_id); 218 | fclose(soc_id); 219 | 220 | if (!strncmp(line_buffer, plat_imx8q, strlen(plat_imx8q)) 221 | || !strncmp(line_buffer, plat_imx8dxl, strlen(plat_imx8dxl))) { 222 | 223 | plat_config_data = &mx8q_boot_config; 224 | plat_config_data->m_u32Arm_type = MX8Q; 225 | return 0; 226 | 227 | } else if (!strncmp(line_buffer, plat_imx8mq, strlen(plat_imx8mq)) 228 | || !strncmp(line_buffer, plat_imx8mm, strlen(plat_imx8mm))) { 229 | 230 | plat_config_data = &mx8mq_boot_config; 231 | plat_config_data->m_u32Arm_type = MX8MQ; 232 | return 0; 233 | } else if (!strncmp(line_buffer, plat_imx8mn, strlen(plat_imx8mn))) { 234 | plat_config_data = &mx8q_boot_config; 235 | plat_config_data->m_u32Arm_type = MX8MN; 236 | return 0; 237 | } else if (!strncmp(line_buffer, plat_imx8mp, strlen(plat_imx8mp))) { 238 | plat_config_data = &mx8q_boot_config; 239 | plat_config_data->m_u32Arm_type = MX8MP; 240 | return 0; 241 | } 242 | 243 | memset(line_buffer, 0, sizeof(line_buffer)); 244 | } 245 | 246 | cpuinfo = fopen("/proc/cpuinfo", "r"); 247 | if (!cpuinfo) { 248 | fprintf(stderr, "Can't open /proc/cpuinfo to" 249 | "discover Boot ROM version.\n"); 250 | exit(1); 251 | } 252 | 253 | for (;;) { 254 | if (!fgets(line_buffer, sizeof(line_buffer), cpuinfo)) 255 | break; 256 | 257 | /* Check if it's revision line */ 258 | if (strncmp(line_buffer, banner, strlen(banner))) { 259 | /* 260 | * Why use the `Hardware` to parse the system type ? 261 | * [1] If boot linux kernel directly from SD card not by uboot, 262 | * the `Revision` will be zero. 263 | * [2] The code does not change the old logic. 264 | */ 265 | if (!strncmp(line_buffer, banner_hw, strlen(banner))) { 266 | rev = strstr(line_buffer, "MX"); 267 | if (rev) { 268 | char tmp[3] = {}; 269 | 270 | rev += 2; 271 | memcpy(tmp, rev, 2); 272 | hw_system_rev = strtoul(tmp, NULL, 16); 273 | } 274 | } 275 | continue; 276 | } 277 | 278 | rev = index(line_buffer, ':'); 279 | 280 | /*check soc_id and revision for mx6sx*/ 281 | 282 | if (rev != NULL) { 283 | rev++; 284 | system_rev = strtoul(rev, NULL, 16); 285 | system_rev = mxc_cpu(system_rev); 286 | if (!system_rev) 287 | system_rev = hw_system_rev; 288 | 289 | switch (system_rev) { 290 | case MX23: 291 | plat_config_data = &mx23_boot_config; 292 | break; 293 | 294 | case MX28: 295 | plat_config_data = &mx28_boot_config; 296 | break; 297 | 298 | case MX53: 299 | if (mxc_cpu_is_rev(system_rev, CHIP_REV_2_0) < 0) 300 | plat_config_data = &mx53to1_boot_config; 301 | else 302 | plat_config_data = &mx53to2_boot_config; 303 | break; 304 | 305 | case MX50: 306 | plat_config_data = &mx50_boot_config; 307 | break; 308 | 309 | case MX6: 310 | case MX6Q: 311 | case MX6DL: 312 | plat_config_data = &mx6q_boot_config; 313 | soc_id = fopen("/sys/devices/soc0/soc_id", "r"); 314 | if (!soc_id) { 315 | exit(1); 316 | } 317 | fgets(line_buffer, sizeof(line_buffer), soc_id); 318 | 319 | if (!strncmp(line_buffer, plat_imx6sx, strlen(plat_imx6sx))) { 320 | revision = fopen("/sys/devices/soc0/revision", "r"); 321 | if (!revision) { 322 | exit(1); 323 | } 324 | fgets(line_buffer, sizeof(line_buffer), revision); 325 | if (!strncmp(line_buffer, "1.0", strlen("1.0")) || 326 | !strncmp(line_buffer, "1.1", strlen("1.1"))) { 327 | plat_config_data = &mx6sx_boot_config; 328 | /* all other revisions should use the latest boot config */ 329 | } else { 330 | plat_config_data = &mx6sx_to_1_2_boot_config; 331 | } 332 | } 333 | 334 | if (!strncmp(line_buffer, plat_imx6ul, strlen(plat_imx6ul)) || 335 | !strncmp(line_buffer, plat_imx6ull, strlen(plat_imx6ull))) 336 | plat_config_data = &mx6ul_boot_config; 337 | break; 338 | 339 | case MX7: 340 | plat_config_data = &mx7d_boot_config; 341 | break; 342 | 343 | default: 344 | fprintf(stderr, "Couldn't find Boot ROM version\n"); 345 | break; 346 | } 347 | 348 | fclose(cpuinfo); 349 | if (plat_config_data) { 350 | plat_config_data->m_u32Arm_type = system_rev; 351 | return 0; 352 | } 353 | return -1; 354 | } 355 | } 356 | fclose(cpuinfo); 357 | return -1; 358 | } 359 | -------------------------------------------------------------------------------- /src/plat_boot_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2015 Freescale Semiconductor, Inc. All Rights Reserved. 3 | */ 4 | 5 | /* 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along 17 | * with this program; if not, write to the Free Software Foundation, Inc., 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 19 | */ 20 | 21 | #ifndef __PLAT_BOOT_CONFIG_H__ 22 | #define __PLAT_BOOT_CONFIG_H__ 23 | 24 | #include 25 | #include 26 | 27 | #include "mtd.h" 28 | 29 | #define CHIP_REV_1_0 0x10 30 | #define CHIP_REV_2_0 0x20 31 | #define CHIP_REV_2_1 0x21 32 | 33 | #define mxc_cpu(c) ((c) >> 12) 34 | #define mxc_cpu_rev(c) ((c) & 0xFF) 35 | #define mxc_cpu_is_rev(c, rev) \ 36 | ((mxc_cpu_rev(c) == rev) ? 1 : ((mxc_cpu_rev(c) < rev) ? -1 : 2)) 37 | 38 | #define MX23 0x23 39 | #define MX28 0x28 40 | #define MX53 0x53 41 | #define MX50 0x50 42 | /* The 3.5.7 kernel uses the 0x6 for MX6Q. */ 43 | #define MX6 0x6 44 | #define MX6Q 0x63 45 | #define MX6DL 0x61 46 | 47 | #define MX7 0x7 48 | 49 | #define MX8Q 0x8 50 | #define MX8MQ 0x81 51 | #define MX8MN 0x82 52 | #define MX8MP 0x83 53 | 54 | typedef struct _platform_config_t { 55 | uint32_t m_u32RomVer; 56 | uint32_t m_u32EnDISBBM; 57 | uint32_t m_u32EnSoftEcc; 58 | uint32_t m_u32EnBootStreamVerify; 59 | uint32_t m_u32UseNfcGeo; 60 | uint32_t m_u32UseMultiBootArea; 61 | uint32_t m_u32UseSinglePageStride; 62 | uint32_t m_u32Arm_type; 63 | uint32_t m_u32DBBT_FingerPrint; 64 | uint32_t m_u32MaxEccStrength; 65 | int (* rom_mtd_init)(struct mtd_data *md, FILE *fp); 66 | int (* rom_mtd_commit_structures)(struct mtd_data *md, FILE *fp, int flags); 67 | } platform_config; 68 | 69 | extern platform_config *plat_config_data; 70 | 71 | extern int discover_boot_rom_version(void); 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /src/rom_nand_hamming_code_ecc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2006-2010 Freescale Semiconductor, Inc. All Rights Reserved. 3 | */ 4 | 5 | /* 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along 17 | * with this program; if not, write to the Free Software Foundation, Inc., 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 19 | */ 20 | 21 | //////////////////////////////////////////////////////////////////////////////// 22 | //! \addtogroup rom_nand_boot 23 | //! @{ 24 | // 25 | // 26 | //! \file rom_nand_hamming_code_ecc.c 27 | //! \brief hamming code ecc functions. 28 | //! 29 | //////////////////////////////////////////////////////////////////////////////// 30 | 31 | //////////////////////////////////////////////////////////////////////////////// 32 | // Includes and external references 33 | //////////////////////////////////////////////////////////////////////////////// 34 | 35 | #include 36 | 37 | #include "rom_nand_hamming_code_ecc.h" 38 | 39 | //////////////////////////////////////////////////////////////////////////////// 40 | //! 41 | //! \brief compares three NCBs and return error if no two are identical else 42 | //! returns the one that matches with at least another copy. 43 | //! 44 | //! a local function 45 | //! 46 | //! This function compares the three NCB structures and their corresponding 47 | //! parity bits. If any two matches then search ends. If none matches then it 48 | //! returns an error. 49 | //! 50 | //! \param[in] pNCBCopy1 pointer to first NCB structure 51 | //! \param[in] pNCBCopy2 pointer to second NCB structure 52 | //! \param[in] pNCBCopy3 pointer to third NCB structure 53 | //! \param[in] pP1 pointer to first set of Parity bits 54 | //! \param[in] pP2 pointer to second set of Parity bits 55 | //! \param[in] pP3 pointer to third set of Parity bits 56 | //! \param[out] pu8HammingCopy to return either 1, 2 or 3 that qualified for 57 | //! Humming code analysis. 58 | //! 59 | //! \retval 0 60 | //! \retval -1 61 | //! 62 | //////////////////////////////////////////////////////////////////////////////// 63 | int TripleRedundancyCheck(uint8_t * pNCBCopy1, uint8_t * pNCBCopy2, 64 | uint8_t * pNCBCopy3, uint8_t * pP1, 65 | uint8_t * pP2, uint8_t * pP3, 66 | uint8_t * pu8HammingCopy) 67 | { 68 | int nError; 69 | 70 | // compare 1 and 2 copies of NCB 71 | nError = 0; 72 | nError = memcmp(pNCBCopy1, pNCBCopy2, NAND_HC_ECC_SIZEOF_DATA_BLOCK_IN_BYTES); 73 | if (nError == 0) 74 | nError = memcmp(pP1, pP2, NAND_HC_ECC_SIZEOF_PARITY_BLOCK_IN_BYTES); 75 | 76 | if (nError == 0) { 77 | // 1 and 2 are identical so lets go with 1 78 | *pu8HammingCopy = 1; 79 | return 0; 80 | } 81 | // compare 1 and 3 copies of NCB 82 | nError = 0; 83 | nError = memcmp(pNCBCopy1, pNCBCopy3, NAND_HC_ECC_SIZEOF_DATA_BLOCK_IN_BYTES); 84 | if (nError == 0) 85 | nError = memcmp(pP1, pP3, NAND_HC_ECC_SIZEOF_PARITY_BLOCK_IN_BYTES); 86 | 87 | if (nError == 0) { 88 | // 1 and 3 are identical so lets go with 1 89 | *pu8HammingCopy = 1; 90 | return 0; 91 | } 92 | // compare 2 and 3 copies of NCB 93 | nError = 0; 94 | nError = memcmp(pNCBCopy2, pNCBCopy3, NAND_HC_ECC_SIZEOF_DATA_BLOCK_IN_BYTES); 95 | if (nError == 0) 96 | nError = memcmp(pP2, pP3, NAND_HC_ECC_SIZEOF_PARITY_BLOCK_IN_BYTES); 97 | 98 | if (nError == 0) { 99 | // 2 and 3 are identical so lets go with 2 100 | *pu8HammingCopy = 2; 101 | return 0; 102 | } 103 | 104 | return -1; 105 | } 106 | 107 | //////////////////////////////////////////////////////////////////////////////// 108 | //! 109 | //! \brief count number of 1s and return true if they occur even number of times 110 | //! in the given byte. 111 | //! 112 | //! xor all the bits of u8, if even number of 1s in u8, then the result is 0. 113 | //! 114 | //! \param[in] u8 // input byte 115 | //! 116 | //! \retval true, if even number of 1s in u8 117 | //! \retval false, if odd number of 1s in u8 118 | //! 119 | //////////////////////////////////////////////////////////////////////////////// 120 | int IsNumOf1sEven(uint8_t u8) 121 | { 122 | int i, nCountOf1s = 0; 123 | 124 | for (i = 0; i < 8; i++) { 125 | nCountOf1s ^= ((u8 & (1 << i)) >> i); 126 | } 127 | return !nCountOf1s; 128 | } 129 | 130 | //////////////////////////////////////////////////////////////////////////////// 131 | //! 132 | //! \brief calculates parity using Hsiao Code and Hamming code 133 | //! 134 | //! \param[in] d, given 16 bits integer 135 | //! \param[out] p, pointer to uint8_t for parity 136 | //! 137 | //! \retval none. 138 | //! 139 | //////////////////////////////////////////////////////////////////////////////// 140 | void CalculateParity(uint16_t d, uint8_t * p) 141 | { 142 | // p[0] = d[15] ^ d[12] ^ d[11] ^ d[ 8] ^ d[ 5] ^ d[ 4] ^ d[ 3] ^ d[ 2]; 143 | // p[1] = d[13] ^ d[12] ^ d[11] ^ d[10] ^ d[ 9] ^ d[ 7] ^ d[ 3] ^ d[ 1]; 144 | // p[2] = d[15] ^ d[14] ^ d[13] ^ d[11] ^ d[10] ^ d[ 9] ^ d[ 6] ^ d[ 5]; 145 | // p[3] = d[15] ^ d[14] ^ d[13] ^ d[ 8] ^ d[ 7] ^ d[ 6] ^ d[ 4] ^ d[ 0]; 146 | // p[4] = d[12] ^ d[ 9] ^ d[ 8] ^ d[ 7] ^ d[ 6] ^ d[ 2] ^ d[ 1] ^ d[ 0]; 147 | // p[5] = d[14] ^ d[10] ^ d[ 5] ^ d[ 4] ^ d[ 3] ^ d[ 2] ^ d[ 1] ^ d[ 0]; 148 | 149 | uint8_t Bit0 = (d & (1 << 0)) ? 1 : 0; 150 | uint8_t Bit1 = (d & (1 << 1)) ? 1 : 0; 151 | uint8_t Bit2 = (d & (1 << 2)) ? 1 : 0; 152 | uint8_t Bit3 = (d & (1 << 3)) ? 1 : 0; 153 | uint8_t Bit4 = (d & (1 << 4)) ? 1 : 0; 154 | uint8_t Bit5 = (d & (1 << 5)) ? 1 : 0; 155 | uint8_t Bit6 = (d & (1 << 6)) ? 1 : 0; 156 | uint8_t Bit7 = (d & (1 << 7)) ? 1 : 0; 157 | uint8_t Bit8 = (d & (1 << 8)) ? 1 : 0; 158 | uint8_t Bit9 = (d & (1 << 9)) ? 1 : 0; 159 | uint8_t Bit10 = (d & (1 << 10)) ? 1 : 0; 160 | uint8_t Bit11 = (d & (1 << 11)) ? 1 : 0; 161 | uint8_t Bit12 = (d & (1 << 12)) ? 1 : 0; 162 | uint8_t Bit13 = (d & (1 << 13)) ? 1 : 0; 163 | uint8_t Bit14 = (d & (1 << 14)) ? 1 : 0; 164 | uint8_t Bit15 = (d & (1 << 15)) ? 1 : 0; 165 | 166 | *p = 0; 167 | 168 | *p |= ((Bit15 ^ Bit12 ^ Bit11 ^ Bit8 ^ Bit5 ^ Bit4 ^ Bit3 ^ Bit2) << 0); 169 | *p |= 170 | ((Bit13 ^ Bit12 ^ Bit11 ^ Bit10 ^ Bit9 ^ Bit7 ^ Bit3 ^ Bit1) << 1); 171 | *p |= 172 | ((Bit15 ^ Bit14 ^ Bit13 ^ Bit11 ^ Bit10 ^ Bit9 ^ Bit6 ^ Bit5) << 2); 173 | *p |= ((Bit15 ^ Bit14 ^ Bit13 ^ Bit8 ^ Bit7 ^ Bit6 ^ Bit4 ^ Bit0) << 3); 174 | *p |= ((Bit12 ^ Bit9 ^ Bit8 ^ Bit7 ^ Bit6 ^ Bit2 ^ Bit1 ^ Bit0) << 4); 175 | *p |= ((Bit14 ^ Bit10 ^ Bit5 ^ Bit4 ^ Bit3 ^ Bit2 ^ Bit1 ^ Bit0) << 5); 176 | } 177 | 178 | //////////////////////////////////////////////////////////////////////////////// 179 | //! \brief pre calculated array of syndromes using Hsiao code. 180 | //! 181 | //! The table consists of 22 entries, first 16 entries for each bit of error in 182 | //! 16-bit data and the next 6 entries for 6-bit parity. 183 | //! 184 | //! The logic used to calculate this table is explained in the code below: 185 | //! \code 186 | //! 187 | //! for(j=0; j<22; j++) { // for each error location 188 | //! 189 | //! // d is 16-bit data and p is 6-bit parity 190 | //! // initialize received vector 191 | //! for(i=0;i<22;i++) { 192 | //! if(i<16) 193 | //! r[i] = d[i]; 194 | //! else 195 | //! r[i] = p[i-16]; 196 | //! } 197 | //! // inject error 198 | //! r[j]=r[j]^0x1; 199 | //! 200 | //! // compute syndrome 201 | //! s[0] = r[16] ^ r[15] ^ r[12] ^ r[11] ^ r[8] ^ r[5] ^ r[4] ^ r[3] ^ r[2]; 202 | //! s[1] = r[17] ^ r[13] ^ r[12] ^ r[11] ^ r[10] ^ r[9] ^ r[7] ^ r[3] ^ r[1]; 203 | //! s[2] = r[18] ^ r[15] ^ r[14] ^ r[13] ^ r[11] ^ r[10] ^ r[9] ^ r[6] ^ r[5]; 204 | //! s[3] = r[19] ^ r[15] ^ r[14] ^ r[13] ^ r[8] ^ r[7] ^ r[6] ^ r[4] ^ r[0]; 205 | //! s[4] = r[20] ^ r[12] ^ r[9] ^ r[8] ^ r[7] ^ r[6] ^ r[2] ^ r[1] ^ r[0]; 206 | //! s[5] = r[21] ^ r[14] ^ r[10] ^ r[5] ^ r[4] ^ r[3] ^ r[2] ^ r[1] ^ r[0]; 207 | //! } 208 | //! \endcode 209 | //////////////////////////////////////////////////////////////////////////////// 210 | static const uint8_t au8SyndTable[] = { 211 | 0x38, 0x32, 0x31, 0x23, 0x29, 0x25, 0x1C, 0x1A, 0x19, 0x16, 0x26, 0x07, 212 | 0x13, 0x0E, 0x2C, 0x0D, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20 213 | }; 214 | 215 | //////////////////////////////////////////////////////////////////////////////// 216 | //! 217 | //! \brief looks up for a match in syndrome table array. 218 | //! 219 | //! \param[in] u8Synd given syndrome to match in the table 220 | //! \param[out] pu8BitToFlip pointer to return the index of array that matches 221 | //! with given syndrome 222 | //! 223 | //! \retval 0 if a match is found 224 | //! \retval -1 no match found 225 | //! 226 | //////////////////////////////////////////////////////////////////////////////// 227 | int TableLookupSingleErrors(uint8_t u8Synd, uint8_t * pu8BitToFlip) 228 | { 229 | uint8_t i; 230 | for (i = 0; i < 22; i++) { 231 | if (au8SyndTable[i] == u8Synd) { 232 | *pu8BitToFlip = i; 233 | return 0; 234 | } 235 | } 236 | return -1; 237 | } 238 | 239 | //////////////////////////////////////////////////////////////////////////////// 240 | //! 241 | //! \brief evaluate NCB block with Hamming Codes 242 | //! 243 | //! This function evaluates NCB Block with Hamming codes and if single bit error 244 | //! occurs then it is fixed, if double error occurs then it returns an error 245 | //! 246 | //! \param[in] pNCB, NCB block 247 | //! \param[in] pParityBlock, block of parity codes, every 6 bits for every 16 bits of 248 | //! data in NCB block 249 | //! 250 | //! \retval 0, if no error or 1 bit error that is fixed. 251 | //! \retval ERROR_ROM_NAND_DRIVER_NCB_HAMMING_DOUBLE_ERROR, double error occured 252 | //! that cannot be fixed. 253 | //! 254 | //////////////////////////////////////////////////////////////////////////////// 255 | int HammingCheck(uint8_t * pNCB, uint8_t * pParityBlock) 256 | { 257 | uint16_t *p16Data = (uint16_t *) pNCB; 258 | uint8_t P; 259 | uint8_t NP; 260 | int i, j = 0; 261 | int nBitPointer = 0; 262 | uint8_t u8Syndrome, u8BitToFlip; 263 | int retStatus = 0; 264 | 265 | P = 0; // kill warning 266 | for (i = 0; i < 256; i++) { 267 | // the problem here is to read 6 bits from an 8-bit byte array for each parity code. 268 | // The parity code is either present in 6 lsbs, or partial in one byte and remainder 269 | // in the next byte or it can be 6 msbs. The code below tries to collect all 6 bits 270 | // into one byte, leaving upper 2 bits 0. 271 | switch (nBitPointer) { 272 | case 0: 273 | // if nBitPointer is 0, that means, we are at the start of a new byte. 274 | // we can straight away read 6 lower bits from the byte for the parity. 275 | P = (pParityBlock[j] & 0x3F); 276 | nBitPointer = 2; 277 | break; 278 | case 2: 279 | // if nBitPointer is 2, that means, we need to read 2 MSb from jth byte of 280 | //parity block and remaining 4 from next byte. 281 | P = ((pParityBlock[j] & 0xC0) >> 6); // read 2 MSbs and moved them to left 6 times 282 | j++; //go to next byte 283 | P |= ((pParityBlock[j] & 0x0F) << 2); // read 4 from LSb, move it up 2 times so that all 6 bits are aligned in a byte. 284 | nBitPointer = 4; 285 | break; 286 | case 4: 287 | // if nBitPointer is 4, that means, we need to read 4 MSbs from jth byte of 288 | //parity block and remaining 2 from next byte. 289 | P = ((pParityBlock[j] & 0xF0) >> 4); // read 4 MSbs and moved them to left 4 times 290 | j++; //goto the next byte 291 | P |= ((pParityBlock[j] & 0x03) << 4); // read 2 LSbs and moved them to right 4 times so that all 6 bits are aligned in a byte 292 | nBitPointer = 6; 293 | break; 294 | case 6: 295 | // if nBitPointer is 6, that means, we need to read 6 MSbs from jth byte of 296 | // parity block 297 | P = ((pParityBlock[j] & 0xFC) >> 2); // read 6 MSbs and moved them to left 2 times. 298 | nBitPointer = 0; 299 | j++; // go to the next byte 300 | break; 301 | }; 302 | 303 | // calculate new parity out of 16-bit data 304 | if ((*p16Data == 0) || (*p16Data == 0xFFFF)) { 305 | // this is for optimization purpose 306 | NP = 0; 307 | } else { 308 | CalculateParity(*p16Data, &NP); 309 | } 310 | 311 | // calculate syndrome by XORing parity read from NAND and new parity NP just calculated. 312 | u8Syndrome = NP ^ P; 313 | 314 | // if syndrome is 0, that means the data is good. 315 | if (u8Syndrome == 0) { 316 | // data is good. fetch next 16bits 317 | p16Data++; 318 | continue; 319 | } 320 | // check for double bit errors, which is the case when we have even number of 1s in the syndrome 321 | if (IsNumOf1sEven(u8Syndrome)) { 322 | // found a double error, can't fix it, return 323 | return -1; 324 | } 325 | 326 | // this is a single bit error and can be fixed 327 | retStatus = TableLookupSingleErrors(u8Syndrome, &u8BitToFlip); 328 | if (retStatus != 0) { 329 | return retStatus; 330 | } 331 | 332 | if (u8BitToFlip < 16) { 333 | // error is in data bit u8BitToFlip, flip that bit to correct it 334 | *p16Data ^= (0x1 << u8BitToFlip); 335 | } else { 336 | // the error is a 1 bit error and is in parity so we do not worry fixing it. 337 | 338 | // u8BitToFlip -= 16; 339 | // 340 | // // error is in parity 341 | // 342 | // switch(nBitPointer) 343 | // { 344 | // case 0: 345 | // // all bits are in j-1 byte (bits 2..7) of parity block. 346 | // pParityBlock[j-1] ^= 1 << (u8BitToFlip+2); 347 | // break; 348 | // case 2: 349 | // // all bits are in j byte (bits 0..5) of parity block 350 | // pParityBlock[j] ^= 1 << (u8BitToFlip); 351 | // break; 352 | // case 4: 353 | // // 2 bits are in j-1 byte (bits 6 and 7) of parity block 354 | // // 4 bits are in j byte (bits 0..3) of parity block 355 | // if( u8BitToFlip < 2 ) 356 | // { 357 | // // either 6 or 7 bit of j-1 byte to flip. 358 | // pParityBlock[j-1] ^= 1 << (u8BitToFlip+6); 359 | // } 360 | // else 361 | // { 362 | // // either one of 0..3 bits of j byte to flip. 363 | // pParityBlock[j] ^= 1 << (u8BitToFlip-2); 364 | // } 365 | // break; 366 | // case 6: 367 | // if( u8BitToFlip < 4 ) 368 | // { 369 | // // 4 bits are in j-1 byte (bits 4 to 7) of parity block 370 | // pParityBlock[j-1] ^= 1 << (u8BitToFlip+4); 371 | // } 372 | // else 373 | // { 374 | // // 2 bits are in j byte (bits 0 and 1) of parity block 375 | // pParityBlock[j] ^= 1 << (u8BitToFlip-4); 376 | // } 377 | // break; 378 | // }; 379 | } 380 | 381 | // fetch next 16bits 382 | p16Data++; 383 | } 384 | return retStatus; 385 | } 386 | 387 | // eof rom_nand_hamming_code_ecc.c 388 | //! @} 389 | -------------------------------------------------------------------------------- /src/rom_nand_hamming_code_ecc.h: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////////////////////////// 2 | //! \addtogroup rom_nand_boot 3 | //! @{ 4 | // 5 | // Copyright (c) 2006 SigmaTel, Inc. 6 | // 7 | //! \file rom_nand_hamming_code_ecc.h 8 | //! \brief This file provides header info for hamming code ecc. 9 | //! 10 | //////////////////////////////////////////////////////////////////////////////// 11 | #ifndef _ROM_NAND_HAMMING_CODE_ECC_H 12 | #define _ROM_NAND_HAMMING_CODE_ECC_H 13 | 14 | #include 15 | 16 | //////////////////////////////////////////////////////////////////////////////// 17 | // Definitions 18 | //////////////////////////////////////////////////////////////////////////////// 19 | //!< Bytes per NCB data block 20 | #define NAND_HC_ECC_SIZEOF_DATA_BLOCK_IN_BYTES (512) 21 | //! Size of a parity block in bytes for all 16-bit data blocks present inside one 512 byte NCB block. 22 | #define NAND_HC_ECC_SIZEOF_PARITY_BLOCK_IN_BYTES ((((512*8)/16)*6)/8) 23 | //! Offset to first copy of NCB in a NAND page 24 | #define NAND_HC_ECC_OFFSET_FIRST_DATA_COPY (0) 25 | //! Offset to second copy of NCB in a NAND page 26 | #define NAND_HC_ECC_OFFSET_SECOND_DATA_COPY (NAND_HC_ECC_OFFSET_FIRST_DATA_COPY+NAND_HC_ECC_SIZEOF_DATA_BLOCK_IN_BYTES) 27 | //! Offset to third copy of NCB in a NAND page 28 | #define NAND_HC_ECC_OFFSET_THIRD_DATA_COPY (NAND_HC_ECC_OFFSET_SECOND_DATA_COPY+NAND_HC_ECC_SIZEOF_DATA_BLOCK_IN_BYTES) 29 | //! Offset to first copy of Parity block in a NAND page 30 | #define NAND_HC_ECC_OFFSET_FIRST_PARITY_COPY (NAND_HC_ECC_OFFSET_THIRD_DATA_COPY+NAND_HC_ECC_SIZEOF_DATA_BLOCK_IN_BYTES) 31 | //! Offset to second copy of Parity block in a NAND page 32 | #define NAND_HC_ECC_OFFSET_SECOND_PARITY_COPY (NAND_HC_ECC_OFFSET_FIRST_PARITY_COPY+NAND_HC_ECC_SIZEOF_PARITY_BLOCK_IN_BYTES) 33 | //! Offset to third copy of Parity block in a NAND page 34 | #define NAND_HC_ECC_OFFSET_THIRD_PARITY_COPY (NAND_HC_ECC_OFFSET_SECOND_PARITY_COPY+NAND_HC_ECC_SIZEOF_PARITY_BLOCK_IN_BYTES) 35 | 36 | #define BITMASK_HAMMINGCHECKED_ALL_THREE_COPIES 0x7 //!< to indicate all three copies of NCB in first page are processed with Hamming codes. 37 | #define BITMASK_HAMMINGCHECKED_FIRST_COPY 0x1 //!< to indicate first copy of NCB is processed with Hamming codes. 38 | #define BITMASK_HAMMINGCHECKED_SECOND_COPY 0x2 //!< to indicate second copy of NCB is processed with Hamming codes. 39 | #define BITMASK_HAMMINGCHECKED_THIRD_COPY 0x4 //!< to indicate third copy of NCB is processed with Hamming codes. 40 | int TripleRedundancyCheck(uint8_t * pNCBCopy1, uint8_t * pNCBCopy2, 41 | uint8_t * pNCBCopy3, uint8_t * pP1, uint8_t * pP2, 42 | uint8_t * pP3, uint8_t * pu8HammingCopy); 43 | int IsNumOf1sEven(uint8_t u8); 44 | void CalculateParity(uint16_t d, uint8_t * p); 45 | int TableLookupSingleErrors(uint8_t u8Synd, uint8_t * pu8BitToFlip); 46 | int HammingCheck(uint8_t * pNCB, uint8_t * pParityBlock); 47 | 48 | #endif /* */ 49 | //////////////////////////////////////////////////////////////////////////////// 50 | // End of file 51 | //////////////////////////////////////////////////////////////////////////////// 52 | // @} 53 | -------------------------------------------------------------------------------- /src/sha.h: -------------------------------------------------------------------------------- 1 | /* 2 | * sha.h 3 | * 4 | * Originally taken from the public domain SHA1 implementation 5 | * written by by Steve Reid 6 | * 7 | * Modified by Aaron D. Gifford 8 | * 9 | * NO COPYRIGHT - THIS IS 100% IN THE PUBLIC DOMAIN 10 | * 11 | * The original unmodified version is available at: 12 | * ftp://ftp.funet.fi/pub/crypt/hash/sha/sha1.c 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef __SHA1_H__ 28 | #define __SHA1_H__ 29 | 30 | #include 31 | #include 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | 37 | /* 38 | * Be sure to get the above definitions right. For instance, on my 39 | * x86 based FreeBSD box, I define LITTLE_ENDIAN and use the type 40 | * "unsigned long" for the quadbyte. On FreeBSD on the Alpha, however, 41 | * while I still use LITTLE_ENDIAN, I must define the quadbyte type 42 | * as "unsigned int" instead. 43 | */ 44 | 45 | #define SHA1_BLOCK_LENGTH 64 46 | #define SHA1_DIGEST_LENGTH 20 47 | 48 | /* The SHA1 structure: */ 49 | typedef struct _SHA_CTX { 50 | uint32_t state[5]; 51 | uint32_t count[2]; 52 | uint8_t buffer[SHA1_BLOCK_LENGTH]; 53 | } SHA_CTX; 54 | 55 | void SHA1_Init(SHA_CTX *context); 56 | void SHA1_Update(SHA_CTX *context, uint8_t *data, unsigned int len); 57 | void SHA1_Final(uint8_t digest[SHA1_DIGEST_LENGTH], SHA_CTX* context); 58 | 59 | #ifdef __cplusplus 60 | } 61 | #endif 62 | 63 | #endif 64 | 65 | -------------------------------------------------------------------------------- /src/sha1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * sha1.c 3 | * 4 | * Originally witten by Steve Reid 5 | * 6 | * Modified by Aaron D. Gifford 7 | * 8 | * NO COPYRIGHT - THIS IS 100% IN THE PUBLIC DOMAIN 9 | * 10 | * The original unmodified version is available at: 11 | * ftp://ftp.funet.fi/pub/crypt/hash/sha/sha1.c 12 | * 13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND 14 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE 17 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 | * SUCH DAMAGE. 24 | */ 25 | 26 | #include 27 | 28 | #include "sha.h" 29 | 30 | #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) 31 | 32 | /* blk0() and blk() perform the initial expand. */ 33 | /* I got the idea of expanding during the round function from SSLeay */ 34 | 35 | #if __BYTE_ORDER == __LITTLE_ENDIAN 36 | #define blk0(i) (block->l[i] = (rol(block->l[i],24)&(uint32_t)0xFF00FF00) \ 37 | |(rol(block->l[i],8)&(uint32_t)0x00FF00FF)) 38 | #else 39 | #define blk0(i) block->l[i] 40 | #endif 41 | 42 | #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ 43 | ^block->l[(i+2)&15]^block->l[i&15],1)) 44 | 45 | /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ 46 | #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); 47 | #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30); 48 | #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); 49 | #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); 50 | #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); 51 | 52 | typedef union _BYTE64QUAD16 { 53 | uint8_t c[64]; 54 | uint32_t l[16]; 55 | } BYTE64QUAD16; 56 | 57 | /* Hash a single 512-bit block. This is the core of the algorithm. */ 58 | void SHA1_Transform(uint32_t state[5], uint8_t buffer[64]) { 59 | uint32_t a, b, c, d, e; 60 | BYTE64QUAD16 *block; 61 | 62 | block = (BYTE64QUAD16*)buffer; 63 | /* Copy context->state[] to working vars */ 64 | a = state[0]; 65 | b = state[1]; 66 | c = state[2]; 67 | d = state[3]; 68 | e = state[4]; 69 | /* 4 rounds of 20 operations each. Loop unrolled. */ 70 | R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); 71 | R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); 72 | R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); 73 | R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); 74 | R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); 75 | R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); 76 | R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); 77 | R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); 78 | R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); 79 | R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); 80 | R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); 81 | R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); 82 | R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); 83 | R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); 84 | R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); 85 | R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); 86 | R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); 87 | R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); 88 | R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); 89 | R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); 90 | /* Add the working vars back into context.state[] */ 91 | state[0] += a; 92 | state[1] += b; 93 | state[2] += c; 94 | state[3] += d; 95 | state[4] += e; 96 | /* Wipe variables */ 97 | a = b = c = d = e = 0; 98 | } 99 | 100 | 101 | /* SHA1_Init - Initialize new context */ 102 | void SHA1_Init(SHA_CTX* context) { 103 | /* SHA1 initialization constants */ 104 | context->state[0] = 0x67452301; 105 | context->state[1] = 0xEFCDAB89; 106 | context->state[2] = 0x98BADCFE; 107 | context->state[3] = 0x10325476; 108 | context->state[4] = 0xC3D2E1F0; 109 | context->count[0] = context->count[1] = 0; 110 | } 111 | 112 | /* Run your data through this. */ 113 | void SHA1_Update(SHA_CTX *context, uint8_t *data, unsigned int len) { 114 | unsigned int i, j; 115 | 116 | j = (context->count[0] >> 3) & 63; 117 | if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++; 118 | context->count[1] += (len >> 29); 119 | if ((j + len) > 63) { 120 | memcpy(&context->buffer[j], data, (i = 64-j)); 121 | SHA1_Transform(context->state, context->buffer); 122 | for ( ; i + 63 < len; i += 64) { 123 | SHA1_Transform(context->state, &data[i]); 124 | } 125 | j = 0; 126 | } 127 | else i = 0; 128 | memcpy(&context->buffer[j], &data[i], len - i); 129 | } 130 | 131 | 132 | /* Add padding and return the message digest. */ 133 | void SHA1_Final(uint8_t digest[SHA1_DIGEST_LENGTH], SHA_CTX *context) { 134 | uint32_t i, j; 135 | uint8_t finalcount[8]; 136 | 137 | for (i = 0; i < 8; i++) { 138 | finalcount[i] = (uint8_t)((context->count[(i >= 4 ? 0 : 1)] 139 | >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ 140 | } 141 | SHA1_Update(context, (uint8_t *)"\200", 1); 142 | while ((context->count[0] & 504) != 448) { 143 | SHA1_Update(context, (uint8_t *)"\0", 1); 144 | } 145 | /* Should cause a SHA1_Transform() */ 146 | SHA1_Update(context, finalcount, 8); 147 | for (i = 0; i < SHA1_DIGEST_LENGTH; i++) { 148 | digest[i] = (uint8_t) 149 | ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); 150 | } 151 | /* Wipe variables */ 152 | i = j = 0; 153 | memset(context->buffer, 0, SHA1_BLOCK_LENGTH); 154 | memset(context->state, 0, SHA1_DIGEST_LENGTH); 155 | memset(context->count, 0, 8); 156 | memset(&finalcount, 0, 8); 157 | } 158 | 159 | -------------------------------------------------------------------------------- /version: -------------------------------------------------------------------------------- 1 | 0.0.1 2 | --------------------------------------------------------------------------------