I want to write to a physical address to change the voltage of a pin using an ARM board- but in order to write to a physical address, I need to take a virtual address, and map it to the physical address using mmap.
So I did that, in this way:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>
#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)
int main(void) {
int fd;
int *map_base_c,*map_base_d, *map_base_p, *virt_addr;
off_t target,control,data,pullup;
control=0x56000050;
if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;
map_base_d = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd,data & ~MAP_MASK);
printf("Memory mapped at address %p.\n", map_base_d);
virt_addr = map_base_d; //+ (data & MAP_MASK)
*virt_addr = 0x00; //This is where it goes off. find out why!!!
printf("Value at address 0x%X (%p): 0x%X\n", data, virt_addr,(*virt_addr));
close(fd);
return 0;
}
But, The pin didn't get a high voltage as I'd expected. Is there something wrong with the way I'm changing the address?
Also, is there a way to see the physical address which was mapped to the virtual address?
Thanks!
In your call to mmap, the offset argument should be the lowest physical address which you want access to. In your code, you pass data & ~MAP_MASK, and data hasn't been initialized (or has been default-initialized to 0).
I believe you want something similar to the following:
uintptr_t control = 0x56000050;
uintptr_t base = control & ~MAP_MASK;
int fd;
void *map_base_d;
int *virt_addr;
if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;
map_base_d = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, base);
//now *map_base_d corresponds to the physical address of 0x56000000
virt_addr = map_base_d + (control - base);
*virt_addr = 0x00; //Make sure virt_addr is a pointer of the right width (int*, char*, etc), so that you don't accidentally write a dword when you really only want to write a single word.
Related
I have a shared library linked to an executable for which I would like to have code coverage instrumentation using custom _sanitizer_cov_trace_pc* functions.
library.cc
#include <stdio.h>
void so_function() {
printf("SO function.");
}
callbacks.cc
#include <stdint.h>
#include <stdio.h>
#include <sanitizer/coverage_interface.h>
extern "C" void __sanitizer_cov_trace_pc_guard_init(uint32_t *start,
uint32_t *stop) {
static uint64_t N;
if (start == stop || *start) return;
printf("INIT: %p %p\n", start, stop);
for (uint32_t *x = start; x < stop; x++)
*x = ++N;
}
extern "C" void __sanitizer_cov_trace_pc_guard(uint32_t *guard) {
if (!*guard) return;
void *PC = __builtin_return_address(0);
char PcDescr[1024];
__sanitizer_symbolize_pc(PC, "%p %F %L", PcDescr, sizeof(PcDescr));
printf("guard: %p %x PC %s\n", guard, *guard, PcDescr);
}
main.cc
#include <stdio.h>
void so_function();
int main(int argc, char **argv) {
so_function();
}
I compiled the library using clang's -fsanitize-coverage=trace-pc-guard into position-independent code (-fPIC) and then I created the shared library using both the resulted object file and callbacks.cc using -fsanitize=address.
I compiled main.cc and linked it with the shared library but it seems like these 2 custom __sanitizer_cov_trace_pc_guard* functions don't get called.
I would like have code coverage instrumentation using these 2 functions only for the shared library, and not for the main executable.
i'am tray to print a strings from user to screen the problem i faced that i nedd when user insert (enter)key the program go to new line and user still have the ability to print on screen , but when i insert (enter)the program go to the first line and overwrite the old words here is my code
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
int len;
char searsh_word[10];
char ch;
printf("enter your strings\n");
while(ch!='EOF'){
ch=getch();
printf("%c",ch);
}
puts("\nEnter the word you need to search for : ");
scanf("%s",searsh_word);
len=strlen(searsh_word);
printf("your word length is : %d",len);
return 0;
}
the problem was by useing getch() you can use grtchar() for better result
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
FILE *file;
int main()
{
int len;
char searsh_word[10];
char ch;
file=fopen("test.txt","w");
if(!file){
printf("Error opening file\n");
exit(1);
}else{
printf("enter your strings\n");
}
do{
ch=getchar();
fprintf(file,"%c",ch);
}while(ch!='.');
fclose(file);
puts("\nEnter the word you need to search for : ");
scanf("%s",searsh_word);
len=strlen(searsh_word);
printf("your word length is : %d",len);
return 0;
}
I am currently implementing openssl into my application. My problem arose when I had to set the hostname, IP - address, and port of the BIO. I have always known ip and hostname to be the same thing. Could someone please explain the difference.
A host name is a combination of the name of your machine and a domain name (e.g. machinename.domain.com). The purpose of a host name is readability - it's much easier to remember than an IP address. All hostnames resolve to IP addresses, so in many instances they are talked about like they are interchangeable.
A host name can have multiple IP addresses, but not the other way around. If you check out
https://beej.us/guide/bgnet/html/multi/gethostbynameman.html
you'll see that gethostbyname() returns a list of addresses for a particular host. To prove it, here's a small program:
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main(int argc, char** argv)
{
if (argc < 2)
{
printf("usage: %s hostname\n", argv[0]);
return 0;
}
struct in_addr addr;
struct hostent* he = gethostbyname(argv[1]);
if (!he)
{
perror("gethostbyname");
return 1;
}
printf("IP addresses for %s:\n\n", he->h_name);
for (int i = 0; he->h_addr_list[i]; i++)
{
memcpy(&addr, he->h_addr_list[i], sizeof(struct in_addr));
printf("%s\n", inet_ntoa(addr));
}
return 0;
}
Entering www.yahoo.com, I get the following:
98.137.246.8
98.137.246.7
98.138.219.232
98.138.219.231
All my OpenCV functions are working perfectly fine. But cvResize() is not found by the compiler. I guess this function is not installed during installation.
The following program tells me the error that cvResize identifier is undefined
Is it possible to download this function separately and use it? How?
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <ctype.h>
#include <iostream>
using namespace std;
int main( int argc, char** argv )
{
// Create an IplImage object *image
IplImage *source = cvLoadImage( argv[1]);
// Here we retrieve a percentage value to a integer
int percent = atoi(argv[3]);
// declare a destination IplImage object with correct size, depth and channels
IplImage *destination = cvCreateImage
( cvSize((int)((source->width*percent)/100) , (int)((source->height*percent)/100) ), source->depth, source->nChannels );
//use cvResize to resize source to a destination image
cvResize(source, destination);
// save image with a name supplied with a second argument
cvSaveImage( argv[2], destination );
return 0;
}
You are missing an include:
#include "opencv2/imgproc/imgproc_c.h"
I fixed the error by
#import <opencv2/imgproc/imgproc_c.h>
I decided to add scripting with Lua. I've downloaded and compiled interpreter. It works fine, but when I want to use any functions from os.* or string.* libs, it says, that "attemt to index global 'os' (a nil value)"
Here is my code and should work, but it does not:
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
extern "C" {
#include "..\liblua\lua.h"
#include "..\liblua\lualib.h"
#include "..\liblua\lauxlib.h"
}
int main(int argc, TCHAR* argv[])
{
lua_State *LuaVM = luaL_newstate();
lua_pushcfunction(LuaVM,luaopen_base);
lua_call(LuaVM,0,0);
lua_pushcfunction(LuaVM,luaopen_math);
lua_call(LuaVM,0,0);
lua_pushcfunction(LuaVM,luaopen_string);
lua_call(LuaVM,0,0);
lua_pushcfunction(LuaVM,luaopen_table);
lua_call(LuaVM,0,0);
int error;
lua_pushstring(LuaVM,"Ver 0.525.5");
lua_setglobal(LuaVM,"Version");
while (true)
{
string strCode;
getline(cin,strCode);
error = luaL_loadbuffer(LuaVM,strCode.c_str(),strCode.length(),"") ||
lua_pcall(LuaVM,0,0,0);
if (error)
{
cout<< lua_tostring(LuaVM,-1)<<endl;
lua_pop(LuaVM,1);
}
}
lua_close(LuaVM);
return 0;
}
What's wrong with it?
In Lua 5.2 the standard luaopen_* functions do not set the corresponding global variables.
Why not copy and adapt the code in linit.c or just call luaL_openlibs?
Otherwise, do what they do: call luaL_requiref for each luaopen_* function.
See http://www.lua.org/source/5.2/linit.c.html#luaL_openlibs.