source: SVN/cambria/redboot/packages/redboot/current/src/swab.c @ 1

Last change on this file since 1 was 1, checked in by Tim Harvey, 2 years ago

restored latest version of files from server backup

Signed-off-by: Tim Harvey <tharvey@…>

File size: 3.7 KB
Line 
1//==========================================================================
2//
3//      swab.c
4//
5//      RedBoot memory byte-swapping (swab) routine
6//
7//==========================================================================
8//####ECOSGPLCOPYRIGHTBEGIN####
9// -------------------------------------------
10// This file is part of eCos, the Embedded Configurable Operating System.
11// Copyright (C) 2005 Red Hat, Inc.
12//
13// eCos is free software; you can redistribute it and/or modify it under
14// the terms of the GNU General Public License as published by the Free
15// Software Foundation; either version 2 or (at your option) any later version.
16//
17// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18// WARRANTY; without even the implied warranty of MERCHANTABILITY or
19// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20// for more details.
21//
22// You should have received a copy of the GNU General Public License along
23// with eCos; if not, write to the Free Software Foundation, Inc.,
24// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25//
26// As a special exception, if other files instantiate templates or use macros
27// or inline functions from this file, or you compile this file and link it
28// with other works to produce a work based on this file, this file does not
29// by itself cause the resulting work to be covered by the GNU General Public
30// License. However the source code for this file must still be made available
31// in accordance with section (3) of the GNU General Public License.
32//
33// This exception does not invalidate any other reasons why a work based on
34// this file might be covered by the GNU General Public License.
35//
36// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37// at http://sources.redhat.com/ecos/ecos-license/
38// -------------------------------------------
39//####ECOSGPLCOPYRIGHTEND####
40//==========================================================================
41//#####DESCRIPTIONBEGIN####
42//
43// Author(s):    msalter
44// Contributors: msalter
45// Date:         2005-02-14
46// Purpose:     
47// Description: 
48//             
49// This code is part of RedBoot (tm).
50//
51//####DESCRIPTIONEND####
52//
53//==========================================================================
54
55#include <redboot.h>
56
57static void
58do_swab(int argc, char *argv[])
59{
60    // Fill a region of memory with a pattern
61    struct option_info opts[4];
62    unsigned long base;
63    long len;
64    bool base_set, len_set;
65    bool set_32bit, set_16bit;
66
67    init_opts(&opts[0], 'b', true, OPTION_ARG_TYPE_NUM, 
68              (void *)&base, (bool *)&base_set, "base address");
69    init_opts(&opts[1], 'l', true, OPTION_ARG_TYPE_NUM, 
70              (void *)&len, (bool *)&len_set, "length");
71    init_opts(&opts[2], '4', false, OPTION_ARG_TYPE_FLG,
72              (void *)&set_32bit, (bool *)0, "fill 32 bit units");
73    init_opts(&opts[3], '2', false, OPTION_ARG_TYPE_FLG,
74              (void *)&set_16bit, (bool *)0, "fill 16 bit units");
75    if (!scan_opts(argc, argv, 1, opts, 4, 0, 0, "")) {
76        return;
77    }
78    if (!base_set || !len_set) {
79        diag_printf("usage: swab -b <addr> -l <length> [-2|-4]\n");
80        return;
81    }
82    if (set_16bit) {
83        // 16 bits at a time
84        while ((len -= sizeof(cyg_uint16)) >= 0) {
85            *(cyg_uint16 *)base = CYG_SWAP16(*(cyg_uint16 *)base);
86            base += sizeof(cyg_uint16);
87        }
88    } else {
89        // Default - 32 bits
90        while ((len -= sizeof(cyg_uint32)) >= 0) {
91            *(cyg_uint32 *)base = CYG_SWAP32(*(cyg_uint32 *)base);
92            base += sizeof(cyg_uint32);
93        }
94    }
95}
96
97RedBoot_cmd("swab", 
98            "Swap bytes in 16-bit or 32-bit words in a block of memory",
99            "-b <location> -l <length> [-2|-4]",
100            do_swab
101    );
102
Note: See TracBrowser for help on using the repository browser.