Changeset 12 for tt-loader/main.c


Ignore:
Timestamp:
May 23, 2008, 12:59:00 PM (16 years ago)
Author:
guillaume
Message:

Publication tt-loader v0.2

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tt-loader/main.c

    r11 r12  
    3333#include <errno.h>
    3434
     35extern unsigned long crc32( unsigned long crc, char* buf, unsigned int len);
     36
    3537/*
    3638 * Here Vendor/Product detected for the target device
     
    4345
    4446/*
    45  * Length of memory dump done by 2nd.bin
     47 * Length of memory dump lines
    4648 */
    47 #define MEM_READ_SIZE           32
     49#define DUMP_LINE_SIZE          32
    4850
    4951struct mem_file {
    5052        void *content ;
    5153        int size ;
     54        char *filename ;
    5255};
    5356
     57/*
     58 * MAX_SIZE is the maximum size of the 2nd.bin program
     59 */
    5460#define MAX_SIZE        65536
     61#define CHUNK_SIZE      8192
    5562static char     buffer[MAX_SIZE + 128];
    5663static int      buffsize = 0;
    57 static struct mem_file cmdfile = { NULL, 0 };
     64static struct mem_file cmdfile = { NULL, 0, NULL };
    5865static double btime ;
    5966static double ticks ;
    6067
    6168#define log1(X)         fprintf(stderr, "%9.3f: "X,((double)times(NULL)-btime)/ticks)
    62 #define log2(X,Y)       fprintf(stderr, "%9.3f: "X,((double)times(NULL)-btime)/ticks,Y)
    63 #define log3(X,Y,Z)     fprintf(stderr, "%9.3f: "X,((double)times(NULL)-btime)/ticks,Y,Z)
     69#define log2(X,Y)       fprintf(stderr, "%9.3f: "X,((double)times(NULL)-btime)/ticks,(Y))
     70#define log3(X,Y,Z)     fprintf(stderr, "%9.3f: "X,((double)times(NULL)-btime)/ticks,(Y),(Z))
     71#define log4(W,X,Y,Z)   fprintf(stderr, "%9.3f: "W,((double)times(NULL)-btime)/ticks,(X),(Y),(Z))
    6472
    6573#if __BYTE_ORDER == __LITTLE_ENDIAN
     
    7179#endif
    7280
    73 static inline unsigned do_div (unsigned v, unsigned d)
     81static inline unsigned roundup (unsigned v, unsigned d)
    7482{
    7583        v+= --d;
     
    92100{
    93101        int     res, len = 5 ;
    94         char buffer[64];
    95102       
    96103        buffer[0]= 'T';
     
    101108        if (src!=NULL)
    102109                len += stringcopy (&buffer[4], src, 60);
     110        else
     111                len = 4 ;
    103112        buffer[63]= '\0' ; // truncate anyway
    104113       
     
    106115        if (res < len)
    107116        {
    108                 fprintf (stderr, "Error in usb_bulk_write during send_char: %d/4\n", res);
     117                log2("Error in usb_bulk_write during send_char: %d/4\n", res);
    109118                return 0;
    110119        }
     
    113122}
    114123
    115 static int send_binsize (usb_dev_handle *handle, int sz)
     124static int send_binsize (usb_dev_handle *handle, int size)
    116125{
    117126        int     res;
    118         char    buffer[8];
    119127        buffer[0]= 'T';
    120128        buffer[1]= 'I';
    121129        buffer[2]= 'S';
    122130        buffer[3]= 's';
    123         *(u_int32_t *) &buffer[4]= cpu_to_le32 ((sz>4096)?4096:sz);
     131        *(u_int32_t *) &buffer[4]= cpu_to_le32 ( size < CHUNK_SIZE ? size : CHUNK_SIZE );
    124132       
    125133        res= usb_bulk_write (handle, OUT_EP, buffer, 8, 1000);
    126134        if (res < 8)
    127135        {
    128                 fprintf (stderr, "Error in usb_bulk_write during send_binsize: %d/8\n", res);
     136                log2("Error in usb_bulk_write during send_binsize: %d/8\n", res);
    129137                return 0;
    130138        }
     
    133141}
    134142
    135 static int send_address (usb_dev_handle *handle, int addr)
     143static int send_word (usb_dev_handle *handle, const char req, int word)
    136144{
    137145        int     res;
    138         char    buffer[8];
    139146        buffer[0]= 'T';
    140147        buffer[1]= 'I';
    141148        buffer[2]= 'S';
    142         buffer[3]= 'a';
    143         *(u_int32_t *) &buffer[4]= cpu_to_le32 (addr);
     149        buffer[3]= req;
     150        *(u_int32_t *) &buffer[4]= cpu_to_le32 (word);
    144151       
    145152        res= usb_bulk_write (handle, OUT_EP, buffer, 8, 1000);
    146153        if (res < 8)
    147154        {
    148                 fprintf (stderr, "Error in usb_bulk_write: %d/8\n", res);
     155                log2("Error in usb_bulk_write: %d/8\n", res);
    149156                return 0;
    150157        }
     
    153160}
    154161
    155 static struct mem_file readfile( const char *filename )
     162static int send_poke (usb_dev_handle *handle, int poke)
    156163{
    157         struct mem_file toread = { NULL, 0 };
     164        int     res;
     165        buffer[0]= 'T';
     166        buffer[1]= 'I';
     167        buffer[2]= 'S';
     168        buffer[3]= 'p';
     169        *(u_int32_t *) &buffer[4]= cpu_to_le32 (poke);
     170       
     171        res= usb_bulk_write (handle, OUT_EP, buffer, 8, 1000);
     172        if (res < 8)
     173        {
     174                log2("Error in usb_bulk_write: %d/8\n", res);
     175                return 0;
     176        }
     177       
     178        return 1;
     179}
     180
     181static struct mem_file readfile( char *filename )
     182{
     183        struct mem_file toread = { NULL, 0, NULL };
    158184        int fd= open (filename, O_RDONLY);
    159185       
    160186        if (fd < 0)
    161187        {
    162                 fprintf (stderr, "Error opening %s file\n", filename);
     188                log3("Error opening %s file (err=%d)\n", filename, errno);
    163189                return toread;
    164190        }
     
    167193        if (toread.size < 0  ||  lseek (fd, 0, SEEK_SET) < 0)
    168194        {
    169                 fprintf (stderr, "Error with lseek other %s file\n", filename);
     195                log3("Error with lseek other %s file (err=%d)\n", filename, errno);
    170196                close (fd);
    171197                return toread;
    172198        }
    173199       
    174         toread.content= malloc (do_div (toread.size, 4));
     200        toread.content= malloc (roundup (toread.size, 4));
    175201        if (toread.content == NULL)
    176202        {
    177                 fprintf (stderr, "Out of memory requesting %d bytes\n", toread.size);
     203                log2("Out of memory requesting %d bytes\n", toread.size);
    178204                close (fd);
    179205                return toread;
     
    182208        if ((toread.size= read (fd, toread.content, toread.size)) < 0)
    183209        {
    184                 fprintf (stderr, "Error reading %s file\n", filename);
     210                log3("Error reading %s file (err=%d)\n", filename, errno);
    185211                close (fd);
    186212                return toread;
     
    190216       
    191217        return toread ;
     218}
     219
     220static int savefile( char *filename, struct mem_file buffer )
     221{
     222        int fd= creat (filename, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH );
     223       
     224        if (fd < 0)
     225        {
     226                log3("Error opening %s file for writing (err=%d)\n", filename, errno);
     227                return 1;
     228        }
     229       
     230        if (buffer.size != write (fd, buffer.content, buffer.size))
     231        {
     232                log3("Error writing %s file (err=%d)\n", filename, errno);
     233                close (fd);
     234                return 1;
     235        }
     236       
     237        close (fd);
     238       
     239        return 0 ;
    192240}
    193241
     
    218266        else
    219267        {
    220                 char    inbuff[256];
    221                 int     insize= usb_bulk_read (handle, IN_EP,
    222                         inbuff, sizeof (inbuff), 5000);
     268                char inbuff[MAX_SIZE + 128];
     269                int     insize= usb_bulk_read (handle, IN_EP, inbuff, sizeof (inbuff), 1000);
    223270               
    224271                if (insize < 48)
     
    235282                        while (size > 0)
    236283                        {
    237                                 int     outsize= usb_bulk_write (handle, OUT_EP, p, 64, 5000);
    238                                 if (outsize < 64)
     284                                int chunksize = size < CHUNK_SIZE ? size : CHUNK_SIZE ;
     285                                int     outsize = usb_bulk_write (handle, OUT_EP, p, chunksize, 1000);
     286                                if (outsize < chunksize)
    239287                                {
    240                                         log2("Error in usb_bulk_write: %d/64\n", outsize);
     288                                        log3("Error in usb_bulk_write: %d/%d\n", outsize,chunksize);
    241289                                        break;
    242290                                }
    243291
    244                                 p+= 64;
    245                                 size-= 64;
     292                                p+= chunksize;
     293                                size-= chunksize;
    246294                        }
    247295                       
     
    249297                        {
    250298                                log2("Sent %d bytes to OMAP\n",buffsize);
    251                                 usb_bulk_write (handle, OUT_EP, buffer, 0, 1000);
    252299                               
    253300                                p= NULL;
    254301                                size= 0;
    255302                               
    256                                 int     res=1, sz= 0, loop= 1, cmd=0 ;
     303                                int     res=1, sz= 0, loop= 1, cmd=0, ftag=0, finfo=0, fline=0 ;
    257304                                int vendor, device, info ;
    258305                                char *cmdp = cmdfile.content ;
    259                                 struct mem_file current = { NULL, 0 };
     306                                struct mem_file current = { NULL, 0, NULL };
    260307                               
    261308                                err = 1 ; // Set error by default
     
    265312                                        if (loop<0) loop++ ; // Avoid infinite loop
    266313                                       
    267                                         if (res>0)
     314                                        if (res>0 || ftag)
    268315                                                res= usb_bulk_read (handle, IN_EP,
    269316                                                        inbuff, sizeof (inbuff), 5000);
     
    275322                                        }
    276323                                       
     324                                        if (ftag)
     325                                        {
     326                                                switch (ftag)
     327                                                {
     328                                                        case 0xaaaa0001:
     329                                                                // A string should be in the buffer with length still set in finfo
     330                                                                if ( res != finfo)
     331                                                                {
     332                                                                        log3("Error waiting string from flasher program: length = %d vs %d expected\n", res, finfo);
     333                                                                        break ;
     334                                                                }
     335                                                                // Fix the string just in case
     336                                                                inbuff[res] = '\0' ;
     337                                                                if (fline)
     338                                                                        fprintf(stderr, "%s",inbuff);
     339                                                                else
     340                                                                        log2("Flasher: %s", inbuff);
     341                                                                if ( inbuff[res-1] == '\n' )
     342                                                                        fline = 0 ;
     343                                                                else
     344                                                                        fline ++ ;
     345                                                                break ;
     346                                                        case 0xaaaa0002:
     347                                                                finfo = le32_to_cpu (*(u_int32_t *) inbuff);
     348                                                                break ;
     349                                                        case 0xaaaa0003:
     350                                                                finfo = le32_to_cpu (*(u_int32_t *) inbuff);
     351                                                                log2("Flasher: NAND read = 0x%08X\n", finfo);
     352                                                                break ;
     353                                                        case 0xaaaa0004:
     354                                                                finfo = le32_to_cpu (*(u_int32_t *) inbuff);
     355                                                                log2("Flasher: NAND size = 0x%08X\n", finfo);
     356                                                                break ;
     357                                                        case 0xaaaa0005:
     358                                                                finfo = le32_to_cpu (*(u_int32_t *) inbuff);
     359                                                                log2("Flasher: Error code = %d\n", finfo);
     360                                                                err = - finfo ; // Will quit on error
     361                                                                break ;
     362                                                        case 0xaaaa0006:
     363                                                                finfo = le32_to_cpu (*(u_int32_t *) inbuff);
     364                                                                log2("Flasher: NAND remain = 0x%08X\n", finfo);
     365                                                                break ;
     366                                                }
     367                                                ftag = 0 ;
     368                                                continue ;
     369                                        }
     370                                       
     371                                        if (err<0)
     372                                        {
     373                                                err = -err ; // Keep err as positive number
     374                                                if (send_cmd (handle,'e',NULL))
     375                                                        log1("Asking stop\n");
     376                                                else
     377                                                        log1("Can't ask to stop\n");
     378                                                break ; // Leave loop
     379                                        }
     380
    277381                                        if (res >= 4)
    278382                                        {
     
    305409                                                                sz= le32_to_cpu (*(u_int32_t *) &inbuff[4]);
    306410                                                                //log2("OMAP read %d\n", sz);                                   // DEBUG
    307                                                                 res= usb_bulk_write (handle, OUT_EP, p, sz, 5000);
     411                                                                res= usb_bulk_write (handle, OUT_EP, p, sz, 1000);
    308412                                                                if (res < sz)
    309413                                                                {
     
    318422                                                                break;
    319423                                                               
     424                                                        case 'x':
     425                                                                info = le32_to_cpu (*(u_int32_t *) &inbuff[4]);
     426                                                                u_int32_t len = le32_to_cpu (*(u_int32_t *) &inbuff[8]);
     427                                                                u_int32_t crc = le32_to_cpu (*(u_int32_t *) &inbuff[12]);
     428                                                                log4("OMAP crc32 computing: pos 0x%08x/0x%08x, current crc32 0x%08X\n",info,len,crc);
     429                                                                break;
     430                                                               
    320431                                                        case 'A':
    321432                                                                info = le32_to_cpu (*(u_int32_t *) &inbuff[4]);
    322433                                                                log2("OMAP address info: 0x%08X\n", info);
     434                                                                break;
     435                                                               
     436                                                        case 'C':
     437                                                                info = le32_to_cpu (*(u_int32_t *) &inbuff[4]);
     438                                                                log2("OMAP crc32 found: 0x%08X\n", info);
    323439                                                                break;
    324440                                                               
     
    343459                                                                break;
    344460                                                               
     461                                                        case 'D':
     462                                                                if (!current.content)
     463                                                                {
     464                                                                        log1("Can't dump memory without buffer\n");
     465                                                                        loop = 0 ;
     466                                                                        break ;
     467                                                                }
     468                                                                res -= 4 ;
     469                                                                if (res < 1)
     470                                                                {
     471                                                                        log2("Received empty memory dump (res=%d)\n",res);
     472                                                                        // reset buffer size anyway
     473                                                                        current.size = 0 ;
     474                                                                        res = 0 ; // Target is waiting a new command
     475                                                                       
     476                                                                } else {
     477                                                                        if (res > size)
     478                                                                        {
     479                                                                                log2("Can't dump %d bytes as buffer is full\n",res);
     480                                                                                loop = 0 ;
     481                                                                                break ;
     482                                                                        }
     483                                                                        memcpy(p,&inbuff[4],res);
     484                                                                        p += res ;
     485                                                                        size -= res ;
     486                                                                }
     487                                                                if ( size == 0 )
     488                                                                {
     489                                                                        log2("%d bytes dumped to buffer\n",current.size);
     490                                                                        res = 0 ; // Target is waiting a new command
     491                                                                }
     492                                                                break;
     493                                                               
    345494                                                        case 'M':
    346                                                                 loop=3 ;
    347                                                                 log2("0x%08X | ",info);
    348                                                                 while (++loop<36)
    349                                                                         fprintf (stderr,"%02hhX", inbuff[loop]);
    350                                                                 loop=3 ;
    351                                                                 fprintf (stderr, " |");
    352                                                                 while (++loop<36)
    353                                                                         if ((int)inbuff[loop]>=0x20 && (int)inbuff[loop]<0x7f)
    354                                                                                 fprintf (stderr,"%c", inbuff[loop]);
    355                                                                         else
    356                                                                                 fprintf (stderr,".");
    357                                                                 fprintf (stderr,"|\n");
     495                                                                sz = 0 ;
     496                                                                res -= 4 ;
     497                                                                while (sz<res)
     498                                                                {
     499                                                                        log2("0x%08X | ",info);
     500                                                                       
     501                                                                        loop=0 ;
     502                                                                        while (++loop<=DUMP_LINE_SIZE)
     503                                                                                fprintf (stderr,"%02hhX", inbuff[3+sz+loop]);
     504                                                                        fprintf (stderr, " |");
     505                                                                       
     506                                                                        loop=0 ;
     507                                                                        while (++loop<=DUMP_LINE_SIZE)
     508                                                                                if ((int)inbuff[3+sz+loop]>=0x20 && (int)inbuff[3+sz+loop]<0x7f)
     509                                                                                        fprintf (stderr,"%c", inbuff[3+sz+loop]);
     510                                                                                else
     511                                                                                        fprintf (stderr,".");
     512                                                                       
     513                                                                        fprintf (stderr,"|\n");
     514                                                                       
     515                                                                        info += DUMP_LINE_SIZE;
     516                                                                        sz += DUMP_LINE_SIZE;
     517                                                                }
    358518                                                                res = 0 ; // Target is waiting a new command
    359                                                                 info += 32 ;
    360519                                                                break;
    361520                                                               
     
    369528                                                                info = le32_to_cpu (*(u_int32_t *) &inbuff[4]);
    370529                                                                log2("OMAP is booting at address: 0x%08X\n", info);
    371                                                                 res = 0 ; // Target is waiting a new command
     530                                                                loop = err = res = 0 ; // Quit now
    372531                                                                break;
    373532
     
    376535                                                                break;
    377536                                                        }
    378                                                 } else {
    379                                                         // Fix the string just in case
    380                                                         inbuff[res] = '\0' ;
    381                                                         log2("Got: %s\n", inbuff);
     537                                                } else
     538                                                {
     539                                                        info = le32_to_cpu (*(u_int32_t *) inbuff);
     540                                                        switch (info)
     541                                                        {
     542                                                                case 0xaaaa0001:
     543                                                                case 0xaaaa0002:
     544                                                                case 0xaaaa0003:
     545                                                                case 0xaaaa0004:
     546                                                                case 0xaaaa0005:
     547                                                                case 0xaaaa0006:
     548                                                                        ftag = info ;
     549                                                                        break ;
     550                                                                case 0xff555580:
     551                                                                        log1("Flasher: Command finished\n");
     552                                                                        break ;
     553                                                                default:
     554                                                                        // Fix the string just in case
     555                                                                        inbuff[res] = '\0' ;
     556                                                                        // Strip final EOL in the string
     557                                                                        if ( inbuff[res-1] == '\n' )
     558                                                                                inbuff[res-1] = '\0' ;
     559                                                                        if (res==4)
     560                                                                                log3("Got: %s (0x%08X)\n", inbuff, info);
     561                                                                        else
     562                                                                                log2("Got: %s\n", inbuff);
     563                                                        }
    382564                                                }
    383565                                        }
     
    397579                                                        switch (cmdp[cmd])
    398580                                                        {
     581                                                        case 'D': // Save downloaded memory to a file still specified
     582                                                                if (current.filename == NULL)
     583                                                                {
     584                                                                        log1("Can't save without a file specified with 'F' command\n");
     585                                                                        loop = 0 ; // Just quit on error
     586                                                                        break ;
     587                                                                }
     588                                                                if (current.content == NULL)
     589                                                                {
     590                                                                        log2("Can't save without download buffer to file '%s'\n",current.filename);
     591                                                                        loop = 0 ; // Just quit on error
     592                                                                        break ;
     593                                                                }
     594                                                                if (current.size==0)
     595                                                                        log2("Nothing to save, file %s is still up to date\n",current.filename);
     596                                                                else if (savefile(current.filename, current)==0)
     597                                                                {
     598                                                                        info = crc32(0,current.content,current.size);
     599                                                                        log3("Downloaded memory saved to file %s with CRC32=0x%08X\n",current.filename,info);
     600                                                                } else
     601                                                                {
     602                                                                        log2("Can't save downloaded memory saved to file %s\n",current.filename);
     603                                                                        // Just quit on error
     604                                                                        loop = 0 ;
     605                                                                }
     606                                                                // Release memory
     607                                                                free((void *)current.content);
     608                                                                free((void *)current.filename);
     609                                                                current.content = NULL ;
     610                                                                current.filename = NULL ;
     611                                                                res -- ;
     612                                                                break;
     613                                                        case 'd': // Save downloaded memory to a file
     614                                                                cmd += 2 ;
     615                                                                if (cmd >= cmdfile.size || cmdp[cmd] == '\0')
     616                                                                {
     617                                                                        log1("Bad 'd' format in command download\n");
     618                                                                        break ;
     619                                                                }
     620                                                                if (current.content == NULL)
     621                                                                {
     622                                                                        log2("Can't save without download buffer to file '%s'\n",&cmdp[cmd]);
     623                                                                        loop = 0 ; // Just quit on error
     624                                                                        break ;
     625                                                                }
     626                                                                if (current.size==0)
     627                                                                        log2("Nothing to save, file %s is still up to date\n",&cmdp[cmd]);
     628                                                                else if (savefile(&cmdp[cmd], current)==0)
     629                                                                {
     630                                                                        info = crc32(0,current.content,current.size);
     631                                                                        log3("Downloaded memory saved to file %s with CRC32=0x%08X\n",&cmdp[cmd],info);
     632                                                                } else
     633                                                                {
     634                                                                        log2("Can't save downloaded memory saved to file %s\n",&cmdp[cmd]);
     635                                                                        // Just quit on error
     636                                                                        loop = 0 ;
     637                                                                }
     638                                                                // Release memory
     639                                                                free((void *)current.content);
     640                                                                current.content = NULL ;
     641                                                                res -- ;
     642                                                                break;
    399643                                                        case 'f': // Put a file to be uploaded in a memory buffer
    400644                                                                cmd += 2 ;
     
    407651                                                                if (current.content != NULL)
    408652                                                                        free((void *)current.content);
    409                                                                 current = readfile((const char *)&cmdp[cmd]);
     653                                                                current = readfile(&cmdp[cmd]);
    410654                                                                if (current.content == NULL)
    411655                                                                {
    412656                                                                        log2("Can't read file '%s'\n",&cmdp[cmd]);
    413657                                                                        loop = 0 ; // Just quit on error
     658                                                                        break ;
    414659                                                                }
    415660                                                                p = current.content ;
     
    425670                                                                }
    426671                                                                break;
     672                                                        case 'F': // Set a filename that would be loaded to check crc and saved
     673                                                                cmd += 2 ;
     674                                                                if (cmd >= cmdfile.size || cmdp[cmd] == '\0')
     675                                                                {
     676                                                                        log1("Bad 'F' format in command file\n");
     677                                                                        break ;
     678                                                                }
     679                                                                // Free previously used memory buffer
     680                                                                if (current.filename != NULL)
     681                                                                        free((void *)current.filename);
     682                                                                info = strlen(&cmdp[cmd]);
     683                                                                if (info == 0)
     684                                                                {
     685                                                                        log2("Can't read filename '%s' length\n",&cmdp[cmd]);
     686                                                                        loop = 0 ; // Just quit on error
     687                                                                        break ;
     688                                                                }
     689                                                                current.filename = malloc (++info);
     690                                                                if (current.filename == NULL)
     691                                                                {
     692                                                                        log2("Out of memory requesting %d bytes\n", info);
     693                                                                        loop = 0 ; // Just quit on error
     694                                                                        break ;
     695                                                                }
     696                                                                if (info!=stringcopy(current.filename,&cmdp[cmd],info))
     697                                                                {
     698                                                                        log2("Can't read filename '%s'\n",&cmdp[cmd]);
     699                                                                        loop = 0 ; // Just quit on error
     700                                                                        break ;
     701                                                                }
     702                                                                // still read next command
     703                                                                res -- ;
     704                                                                break;
     705                                                        case 'C': // Get a CRC32 and send it to target
     706                                                                cmd += 2 ;
     707                                                                if (sscanf((const char *)&cmdp[cmd], "%i", &info) != 1)
     708                                                                {
     709                                                                        log2("Can't read crc32 '%s'\n",&cmdp[cmd]);
     710                                                                        loop = 0 ; // Just quit on error
     711                                                                        break ;
     712                                                                }
     713                                                                // compute the current CRC32 if a file is referenced and exists
     714                                                                if (!info && current.filename != NULL)
     715                                                                {
     716                                                                        struct mem_file loader = readfile(current.filename);
     717                                                                        if (loader.content == NULL)
     718                                                                        {
     719                                                                                log2("Can't read file '%s', will be initialized with target\n",current.filename);
     720                                                                        } else
     721                                                                        {
     722                                                                                log3("Checking CRC32 of %d bytes from %s file\n",loader.size,current.filename);
     723                                                                                info = crc32(0,loader.content,loader.size);
     724                                                                                free(loader.content);
     725                                                                        }
     726                                                                }
     727                                                                if (send_word (handle, 'C', info))
     728                                                                        log2("Sending crc32 0x%02X\n", info);
     729                                                                else
     730                                                                {
     731                                                                        log2("Can't send crc32 0x%02X\n", info);
     732                                                                        loop = 0 ; // Just quit on error
     733                                                                }
     734                                                                break;
    427735                                                        case 'a': // Get an address and send it to target
    428736                                                                cmd += 2 ;
     
    431739                                                                        log2("Can't read address '%s'\n",&cmdp[cmd]);
    432740                                                                        loop = 0 ; // Just quit on error
    433                                                                 }
    434                                                                 if (send_address (handle, info))
     741                                                                        break ;
     742                                                                }
     743                                                                if (send_word (handle, 'a', info))
    435744                                                                        log2("Sending address 0x%02X\n", info);
    436745                                                                else
     
    440749                                                                }
    441750                                                                break;
     751                                                        case 'm': // Get a size in kB and ask a memory download
     752                                                                cmd += 2 ;
     753                                                                if (sscanf((const char *)&cmdp[cmd], "%i", &info) != 1)
     754                                                                {
     755                                                                        log2("Can't read size '%s'\n",&cmdp[cmd]);
     756                                                                        loop = 0 ; // Just quit on error
     757                                                                        break ;
     758                                                                }
     759                                                                // size unit is kbytes
     760                                                                size = info * 1024 ;
     761                                                                // Free previously used memory buffer
     762                                                                if (current.content != NULL)
     763                                                                        free((void *)current.content);
     764                                                                current.size = size ;
     765                                                                p = malloc (roundup (size, 4));
     766                                                                if ( p == NULL)
     767                                                                {
     768                                                                        log2("Out of memory requesting %d bytes\n", roundup (size, 4));
     769                                                                        loop=0 ;
     770                                                                        break ;
     771                                                                }
     772                                                                current.content = p ;
     773                                                                if (!(send_word (handle,'m',size)))
     774                                                                {
     775                                                                        log2("Can't ask to download %d bytes from memory\n",size);
     776                                                                        loop = 0 ; // Just quit on error
     777                                                                } else
     778                                                                        log2("Allocated %d kBytes to download memory\n",info);
     779                                                                break;
    442780                                                        case 'M': // Ask memory dump
    443781                                                                if (!(send_cmd (handle,'M',NULL)))
    444782                                                                {
    445783                                                                        log1("Can't ask to read memory\n");
     784                                                                        loop = 0 ; // Just quit on error
     785                                                                }
     786                                                                break;
     787                                                        case 'P': // Ask a peek onto the address
     788                                                                if (!(send_cmd (handle,'P',NULL)))
     789                                                                {
     790                                                                        log1("Can't ask to peek memory\n");
     791                                                                        loop = 0 ; // Just quit on error
     792                                                                }
     793                                                                break;
     794                                                        case 'S': // Ask to change stack to the last given address
     795                                                                if (send_cmd (handle,'S',NULL))
     796                                                                        log1("Asking stack relocate\n");
     797                                                                else
     798                                                                {
     799                                                                        log1("Can't ask to relocate stack\n");
    446800                                                                        loop = 0 ; // Just quit on error
    447801                                                                }
     
    457811                                                                break;
    458812                                                        case 'b': // Boot the target by just branching to the last given address
    459                                                                 if (send_cmd (handle,'b',NULL))
     813                                                                if (send_cmd (handle,'b',NULL)) {
    460814                                                                        log1("Asking boot\n");
    461                                                                 else
     815                                                                        // Quit now witout error set
     816                                                                        loop = err = -10 ;
     817                                                                        cmd = cmdfile.size ;
     818                                                                        res = 1 ;
     819                                                                } else
    462820                                                                {
    463821                                                                        log1("Can't ask to boot\n");
     
    467825                                                        case 'e': // End of commands
    468826                                                                log1("Commands read and sent\n");
     827                                                                if (send_cmd (handle,'e',NULL))
     828                                                                        log1("Asking stop\n");
     829                                                                else
     830                                                                        log1("Can't ask to stop\n");
    469831                                                                // Quit now witout error set
    470832                                                                loop = err = 0 ;
     
    472834                                                                res = 1 ;
    473835                                                                break ;
     836                                                        case 'k': // Boot a linux kernel to the last given address
     837                                                                cmd += 2 ;
     838                                                                if (sscanf((const char *)&cmdp[cmd], "%i", &info) != 1)
     839                                                                {
     840                                                                        log2("Can't read mach id '%s'\n",&cmdp[cmd]);
     841                                                                        loop = 0 ; // Just quit on error
     842                                                                        break ;
     843                                                                }
     844                                                                if (send_word (handle,'k', info)) {
     845                                                                        log1("Asking kernel to boot\n");
     846                                                                        // Quit now witout error set
     847                                                                        loop = err = -10 ;
     848                                                                        cmd = cmdfile.size ;
     849                                                                        res = 1 ;
     850                                                                } else
     851                                                                {
     852                                                                        log1("Can't ask to boot a kernel\n");
     853                                                                        loop = 0 ; // Just quit on error
     854                                                                }
     855                                                                break;
     856                                                        case 'p': // poke
     857                                                                cmd += 2 ;
     858                                                                if (sscanf((const char *)&cmdp[cmd], "%i", &info) != 1)
     859                                                                {
     860                                                                        log2("Can't read value to poke '%s'\n",&cmdp[cmd]);
     861                                                                        loop = 0 ; // Just quit on error
     862                                                                        break ;
     863                                                                }
     864                                                                if (send_poke (handle, info))
     865                                                                        log2("Sending poke 0x%08X\n", info);
     866                                                                else
     867                                                                {
     868                                                                        log2("Can't send poke 0x%08X\n", info);
     869                                                                        loop = 0 ; // Just quit on error
     870                                                                }
     871                                                                break;
    474872                                                        case '#':
     873                                                                cmd += 2 ;
     874                                                                log2("%s\n", &cmdp[cmd]);
    475875                                                        case '/':
    476876                                                                // skip comments
     
    490890                                        }
    491891                                }
     892                                // Free used memory buffer
     893                                if (current.content != NULL)
     894                                        free((void *)current.content);
     895                                if (current.filename != NULL)
     896                                        free((void *)current.filename);
    492897                        }
    493898                }
     
    495900       
    496901        if (err>=0) {
     902                log1("Releasing usb interface\n");
    497903                err = usb_release_interface (handle, 0);
    498904                if (err < 0)
    499905                        log2("Error in usb_release_interface (%d)\n",err);
     906                log1("Released usb interface\n");
    500907        }
    501908       
     
    551958        }
    552959       
    553         size= do_div (size, 4);
     960        size= roundup (size, 4);
    554961       
    555962        p[0x21]= cpu_to_le32 (size - 0x40);
     
    562969        p[0x12]= cpu_to_le32 (size);
    563970       
    564         buffsize= 128 + do_div (size, 4);
     971        buffsize= 128 + roundup (size, 4);
    565972       
    566973        cmdfile = readfile(argv[ARG_CMDFILE]);
     
    575982                        if (cmdbuffer[i] == '\n' || cmdbuffer[i] == '\r' || cmdbuffer[i] == ';')
    576983                                cmdbuffer[i] = '\0' ;
     984                        // Skip comment line
     985                        if (cmdbuffer[i] == '/') {
     986                                while ( i++ < cmdfile.size ) {
     987                                        if (cmdbuffer[i] == '\n' || cmdbuffer[i] == '\r') {
     988                                                i-- ;
     989                                                break ;
     990                                        }
     991                                }
     992                        }
    577993                } while ( i++ < cmdfile.size );
    578994        }
Note: See TracChangeset for help on using the changeset viewer.