Skip to content

Instantly share code, notes, and snippets.

@bluesquall
Last active November 6, 2015 23:46
Show Gist options
  • Select an option

  • Save bluesquall/431faf540fee022bb7bc to your computer and use it in GitHub Desktop.

Select an option

Save bluesquall/431faf540fee022bb7bc to your computer and use it in GitHub Desktop.
Tiny script patching devmem2 in LTIB for Phytec LPC3250 on MBARI LRAUV
#!/bin/bash
# Based on https://gist.github.com/the-admax/684d56db33f2f0792b69
# Adapt to your environment
LTIB_ROOT=/opt/ltib
#####################################
if [ "$USER" == 0 ]; then
echo DO NOT RUN THIS SCRIPT AS ROOT
exit 1
fi
# Change dir to one with packages
pushd $LTIB_ROOT/pkgs
# (Taken from http://patchwork.openwrt.org/patch/4062/ )
cat << 'DEVMEM2_PATCH' > devmem2-showBits.patch
--- a/devmem2.c
+++ b/devmem2.c
@@ -20,7 +20,7 @@
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
- *
+ *
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
@@ -38,25 +38,28 @@
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>
-
+
#define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
__LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
-
+
#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)
+void show_bits(unsigned long value, char access_type);
+
int main(int argc, char **argv) {
- int fd;
- void *map_base, *virt_addr;
+ int fd;
+ void *map_base, *virt_addr;
unsigned long read_result, writeval;
off_t target;
int access_type = 'w';
-
+
if(argc < 2) {
- fprintf(stderr, "\nUsage:\t%s { address } [ type [ data ] ]\n"
+ fprintf(stderr, "\nUsage:\t%s { address } [ type [ data [showBits] ] ]\n"
"\taddress : memory address to act upon\n"
"\ttype : access operation type : [b]yte, [h]alfword, [w]ord\n"
- "\tdata : data to be written\n\n",
+ "\tdata : data to be written\n"
+ "\tshowBits: if provided, display bits\n\n",
argv[0]);
exit(1);
}
@@ -67,15 +70,15 @@ int main(int argc, char **argv) {
if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;
- printf("/dev/mem opened.\n");
+ printf("/dev/mem opened.\n");
fflush(stdout);
-
+
/* Map one page */
map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
if(map_base == (void *) -1) FATAL;
- printf("Memory mapped at address %p.\n", map_base);
+ printf("Memory mapped at address %p.\n", map_base);
fflush(stdout);
-
+
virt_addr = map_base + (target & MAP_MASK);
switch(access_type) {
case 'b':
@@ -91,7 +94,10 @@ int main(int argc, char **argv) {
fprintf(stderr, "Illegal data type '%c'.\n", access_type);
exit(2);
}
- printf("Value at address 0x%X (%p): 0x%X\n", target, virt_addr, read_result);
+ printf("Value at address 0x%X (%p): 0x%X\n", target, virt_addr, read_result);
+ if(argc > 4) {
+ show_bits(read_result,access_type);
+ }
fflush(stdout);
if(argc > 3) {
@@ -110,12 +116,49 @@ int main(int argc, char **argv) {
read_result = *((unsigned long *) virt_addr);
break;
}
- printf("Written 0x%X; readback 0x%X\n", writeval, read_result);
+ printf("Written 0x%X; readback 0x%X\n", writeval, read_result);
+ if(argc > 4) {
+ show_bits(read_result,access_type);
+ }
fflush(stdout);
}
-
+
if(munmap(map_base, MAP_SIZE) == -1) FATAL;
close(fd);
return 0;
}
+void show_bits(unsigned long value, char access_type){
+ int i, bits;
+ switch(access_type) {
+ case 'b':
+ bits = 8;
+ break;
+ case 'h':
+ bits = 16;
+ break;
+ case 'w':
+ bits = 32;
+ }
+ for (i = bits-1; i>=0 ; --i){
+ if((i+1)%4==0){
+ printf(" ");
+ }
+ printf("%01d",i / 10);
+ }
+ printf("\n");
+ for (i = bits-1; i>=0 ; --i){
+ if((i+1)%4==0){
+ printf(" ");
+ }
+ printf("%01d",i % 10);
+ }
+ printf("\n");
+ for (i = bits-1; i>=0 ; --i){
+ if((i+1)%4==0){
+ printf(" ");
+ }
+ printf("%01d", value>>i & 1);
+ }
+ printf("\n");
+}
DEVMEM2_PATCH
md5sum devmem2-showBits.patch > devmem2-showBits.patch.md5
# Return to dir with ltib
popd
# Fix devmem2.spec file
patch -p1 << 'SPEC_PATCH'
--- a/dist/lfs-5.1/devmem2/devmem2.spec
+++ b/dist/lfs-5.1/devmem2/devmem2.spec
@@ -9,7 +9,7 @@ Vendor : Freescale
Packager : Ross Wille
Group : Applications/System
Source : devmem2.tar.gz
-Patch0 : devmem2-fixups-2.patch
+Patch0 : devmem2-showBits.patch
BuildRoot : %{_tmppath}/%{name}
Prefix : %{pfx}
@@ -27,7 +27,7 @@ so use this utility with extreme caution!
%patch0 -p1
%Build
-make CFLAGS="-DFORCE_STRICT_ALIGNMENT"
+make
%Install
rm -rf $RPM_BUILD_ROOT
SPEC_PATCH
echo Ready to continue! Now run
echo
echo ./ltib
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment