Home | History | Annotate | Download | only in nandwrite
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *  * Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  *  * Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in
     12  *    the documentation and/or other materials provided with the
     13  *    distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <boot/boot.h>
     30 
     31 #define STATUS_NOMSG 0
     32 #define STATUS_OKAY  1
     33 #define STATUS_FAIL  2
     34 #define STATUS_PRINT 3
     35 
     36 void jtag_hook();
     37 
     38 volatile unsigned _jtag_cmd = 0;
     39 volatile unsigned _jtag_msg = 0;
     40 unsigned char _jtag_cmd_buffer[128];
     41 unsigned char _jtag_msg_buffer[128];
     42 
     43 volatile unsigned _jtag_arg0 = 0;
     44 volatile unsigned _jtag_arg1 = 0;
     45 volatile unsigned _jtag_arg2 = 0;
     46 
     47 static void jtag_msg(unsigned status, const char *msg)
     48 {
     49     unsigned char *out = _jtag_msg_buffer;
     50     while((*out++ = *msg++) != 0) ;
     51     _jtag_msg = status;
     52     do {
     53         jtag_hook();
     54     } while(_jtag_msg != 0);
     55 }
     56 
     57 void jtag_okay(const char *msg)
     58 {
     59     if(msg == 0) msg = "OKAY";
     60     jtag_msg(STATUS_OKAY, msg);
     61 }
     62 
     63 void jtag_fail(const char *msg)
     64 {
     65     if(msg == 0) msg = "FAIL";
     66     jtag_msg(STATUS_FAIL, msg);
     67 }
     68 
     69 int jtag_cmd_pending()
     70 {
     71     jtag_hook();
     72     return (int) _jtag_cmd;
     73 }
     74 
     75 void jtag_cmd_loop(void (*do_cmd)(const char *, unsigned, unsigned, unsigned))
     76 {
     77     unsigned n;
     78     for(;;) {
     79         if(jtag_cmd_pending()){
     80             do_cmd((const char*) _jtag_cmd_buffer, _jtag_arg0, _jtag_arg1, _jtag_arg2);
     81             for(n = 0; n < 256; n++) _jtag_cmd_buffer[n] = 0;
     82             _jtag_arg0 = 0;
     83             _jtag_arg1 = 0;
     84             _jtag_arg2 = 0;
     85             _jtag_cmd = 0;
     86         }
     87     }
     88 }
     89 
     90 static char jtag_putc_buffer[128];
     91 static unsigned jtag_putc_count = 0;
     92 
     93 static void jtag_push_buffer(void)
     94 {
     95     jtag_putc_buffer[jtag_putc_count] = 0;
     96     jtag_putc_count = 0;
     97     jtag_msg(STATUS_PRINT, jtag_putc_buffer);
     98 }
     99 
    100 void jtag_dputc(unsigned c)
    101 {
    102     if((c < 32) || (c > 127)) {
    103         if(c == '\n') {
    104             jtag_push_buffer();
    105         }
    106         return;
    107     }
    108 
    109     jtag_putc_buffer[jtag_putc_count++] = c;
    110     if(jtag_putc_count == 127) {
    111         jtag_push_buffer();
    112     }
    113 }
    114 
    115