Hi Daniel,

may I make some minor code "donations"?

For me "hexout" does not have enough features, because I'm using a "big" 
MSP430.  Therefor I need correct address extensions.  My implementation 
of hexout looks like this:

<snip>
int cmd_hexout(char **arg)
{
        char *off_text = get_arg(arg);
        char *len_text = get_arg(arg);
        char *filename = *arg;
        address_t off;
        address_t length;
        FILE *file;

        if (!(off_text && len_text && *filename)) {
                printc_err("hexout: need offset, length and filename\n");
                return -1;
        }

        if (expr_eval(off_text, &off) < 0 ||
            expr_eval(len_text, &length) < 0)
                return -1;

        // open file
        {
           char * path = NULL;

           path = expand_tilde(filename);
           if (path == NULL)
              return -1;

           file = fopen(path, "w");
           free(path);

           if (file == NULL) {
              pr_error("hexout: couldn't open output file");
              return -1;
           }
        }

        {
           uint16_t off_high_old = 0xffff;
            uint16_t off_buf = 0xffff;

           while (length != 0) {
              uint16_t off_high;
              uint16_t chk;
              uint16_t cnt;
               uint8_t  buf[4096];

              off_high = off >> 16;
              if (off_high != off_high_old) {
                 chk = 2 + 4 + ((off_high >> 8) & 0xff) + (off_high & 0xff);
                 chk = (4096 - chk) & 0xff;
                 fprintf( file, ":02000004%04X%02X\n", off_high, chk );
                 off_high_old = off_high;
              }

              cnt = 16 - (off & 0x0f);
              if (cnt > length) {
                 cnt = length;
              }
              if (cnt > 0) {
                 chk = cnt + (off & 0xff) + (off >> 8);
                 fprintf( file, ":%02X%04X00", cnt, off & 0xffff );

                 while (cnt-- > 0) {
                    if (off_buf >= sizeof(buf)) {
                       int count;

                       count = length;
                       if (count > sizeof(buf)) {
                          count = sizeof(buf);
                       }
                       printc_dbg("Reading %4d bytes from 0x%05x...\n", count, 
off);
                       if (device_readmem(off, buf, count) < 0) {
                          pr_error("hexout: can't read memory");
                          goto fail;
                       }

                       off_buf = 0;
                    }

                    chk += buf[ off_buf ];
                    fprintf( file, "%02X", buf[ off_buf ] );

                    ++off;
                    ++off_buf;
                    --length;
                 }
                 fprintf( file, "%02X\n", (65536-chk) & 0xff );
              }
           }

           // EOF!
           fprintf( file, ":00000001FF\n" );
        }

        if (fclose(file) < 0) {
                pr_error("hexout: error on close");
                return -1;
        }

        return 0;

fail:
        fclose(file);
        unlink(filename);
        return -1;
}
<snap>

It also handles odd start addresses nicer and adds the ihex termination. 
  Of course the 16byte width could be coded more nicely...

Sometimes I also need binary dumps.  My solution:

<snip>
int cmd_binout(char **arg)
{
    char *off_text = get_arg(arg);
    char *len_text = get_arg(arg);
    char *filename = *arg;
    address_t off;
    address_t length;
    int file;

    if (!(off_text && len_text && *filename)) {
       printc_err("binout: need offset, length and filename\n");
       return -1;
    }

    if (expr_eval(off_text, &off) < 0 ||
          expr_eval(len_text, &length) < 0)
       return -1;

    // open file
    {
       char * path = NULL;

       path = expand_tilde(filename);
       if (path == NULL)
          return -1;

       file = open( path, O_WRONLY | O_CREAT | O_BINARY | O_TRUNC, 0666 );
       free(path);

       if (file == -1) {
          pr_error("binout: couldn't open output file");
          return -1;
       }
    }

    // dump memory area
    {
       while (length != 0) {
          uint8_t buf[4096];
          int count;

          count = length;
          if (count > sizeof(buf)) {
             count = sizeof(buf);
          }
          printc_dbg("Reading %4d bytes from 0x%05x...\n", count, off);
          if (device_readmem(off, buf, count) < 0) {
             pr_error("binout: can't read memory");
             goto fail;
          }

          if (write(file, buf, count) != count) {
             pr_error( "binout: cannot write output file" );
             goto fail;
          }

          length -= count;
          off    += count;
       }
    }

    close(file);
    return 0;

fail:
    close(file);
    unlink(filename);
    return -1;
}
<snap>

Of course it must be correctly added at some other places.

Hardy

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Mspgcc-users mailing list
Mspgcc-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mspgcc-users

Reply via email to