⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 e100boot.c

📁 linux平台上的开放源代码的网络摄像机程序.实现视频捕捉,传输以及云台控制等.非常具有参考价值.
💻 C
📖 第 1 页 / 共 4 页
字号:
    }    else if (strncmp(argVect[argi], "--pause", 7) == 0) {      boot_cmds[boot_cmds_cnt++] = PAUSE_LOOP;      GetNumberOption(&argi, argCount, argVect, &boot_cmds[boot_cmds_cnt++], "--pause", 16);    }    else if (strncmp(argVect[argi], "--memtest", 9) == 0) {      boot_cmds[boot_cmds_cnt++] = MEM_TEST;      GetNumberOption(&argi, argCount, argVect, &boot_cmds[boot_cmds_cnt++], "--memtest", 16);      GetNumberOption(&argi, argCount, argVect, &boot_cmds[boot_cmds_cnt++], "--memtest", 16);    }    else if (strncmp(argVect[argi], "--memdump", 9) == 0) {      boot_cmds[boot_cmds_cnt++] = MEM_DUMP;      GetNumberOption(&argi, argCount, argVect, &boot_cmds[boot_cmds_cnt++], "--memdump", 16);      GetNumberOption(&argi, argCount, argVect, &boot_cmds[boot_cmds_cnt++], "--memdump", 16);    }    else if (strncmp(argVect[argi], "--memclear", 10) == 0) {      boot_cmds[boot_cmds_cnt++] = MEM_CLEAR;      GetNumberOption(&argi, argCount, argVect, &boot_cmds[boot_cmds_cnt++], "--memclear", 16);      GetNumberOption(&argi, argCount, argVect, &boot_cmds[boot_cmds_cnt++], "--memclear", 16);    }    else if (strncmp(argVect[argi], "--flash", 7) == 0) {	    boot_cmds[boot_cmds_cnt++] = FLASH;	    GetNumberOption(&argi, argCount, argVect, &boot_cmds[boot_cmds_cnt++], "--flash", 16);	    GetNumberOption(&argi, argCount, argVect, &boot_cmds[boot_cmds_cnt++], "--flash", 16);	    GetNumberOption(&argi, argCount, argVect, &boot_cmds[boot_cmds_cnt++], "--flash", 16);	    doing_flash = TRUE;    }    else if (strncmp(argVect[argi], "--jump", 6) == 0) {      boot_cmds[boot_cmds_cnt++] = JUMP;      GetNumberOption(&argi, argCount, argVect, &boot_cmds[boot_cmds_cnt++], "--jump", 16);    }    else if (strncmp(argVect[argi], "--file", 6) == 0) {      char str[256];      boot_cmds[boot_cmds_cnt++] = PACKET_INFO;      GetStringOption(&argi, argCount, argVect,                       str, "--file");	/* file name */      GetNumberOption(&argi, argCount, argVect,                       &boot_cmds[boot_cmds_cnt++], str, 16); /* address */      last_boot_file = allocate_boot_file(last_boot_file);      first_boot_file = first_boot_file ? first_boot_file : last_boot_file;      last_boot_file->fileName = malloc(strlen(str) + 1);      strcpy(last_boot_file->fileName, str);      last_boot_file->size_p = &boot_cmds[boot_cmds_cnt];      last_boot_file->size_sent = 0;      if (strncmp(str, "-", 1) != 0) {        if (stat(last_boot_file->fileName, &st) == -1) {          printf("Cannot get size of file '%s'. %s.\n",                  last_boot_file->fileName, strerror(errno));          exit(1);        }        printf("* size is %d 0x%8.8x\n",                (int)st.st_size, (unsigned int)st.st_size);        last_boot_file->size = st.st_size;        boot_cmds[boot_cmds_cnt++] = st.st_size;      }      else {        GetNumberOption(&argi, argCount, argVect,                         &last_boot_file->size , str, 16); /* size */                boot_cmds[boot_cmds_cnt++] = last_boot_file->size;        printf("* size is %d 0x%8.8x\n", last_boot_file->size,                last_boot_file->size);              }      D();    }    else if (strncmp(argVect[argi], "--bootfile", 10) == 0) {      GetStringOption(&argi, argCount, argVect,                       boot_loader_file, "--bootfile");    }    else if (strncmp(argVect[argi], "-d", 2) == 0) {      if (GetNumberOption (&argi, argCount, argVect, &number, "-d", 10) == 0) {        printHelp = TRUE;      }      else {        sprintf(dbStr, "%d", number);        for (i = 0; i != (int) strlen(dbStr); i++) {          switch (dbStr[i] - '0') {          case 1:            db1 = TRUE;            break;          case 2:            db2 = TRUE;            break;          case 3:            db3 = TRUE;            break;          case 4:            db4 = TRUE;            break;          default:            printf("ERROR! Debug level %d is not defined.\n", dbStr[i] - '0');            printHelp = TRUE;            break;          }        }      }    }    else if (strncmp(argVect[argi], "--help", 6) == 0) {            printHelp = TRUE;    }        else {      printf("ERROR! Don't understand option '%s'\n", argVect[argi]);      printHelp = TRUE;    }  }  D();  if (printHelp == TRUE) {    PrintHelp();    exit(0);  }}/*****************************************************************************#*#  FUNCTION NAME: GetStringOption*#*#  PARAMETERS: int *argNr      : Returns next argc here. *#              int argCount    : Index of last argument.*#              char *argVect[] : argv.*#              char *ret       : Copy string here.*#              char *option    : Name of the option.*#*#  DESCRIPTION: Extracts a string option from argv, and updates argnr.*#  Returns TRUE/FALSE and string in *ret.*#*#---------------------------------------------------------------------------*#  HISTORY*#*#  DATE     NAME     CHANGES*#  ----     ----     -------*#  960930   ronny    Initial version*#  961203   ronny    Handles filenames with spaces within 'file name'.*#*#***************************************************************************/intGetStringOption(int *argNr, int argCount, char *argVect[], char *ret,		char *option){  int startChar = strlen(option);  *ret = '\0';  /* Are there any more chars after option? If not skip to next argv. */  if (strlen(argVect[*argNr]) <= (unsigned int)startChar) {    (*argNr)++;    startChar = 0;  }  /* Any args left? */  if (*argNr >= argCount) {    printf("ERROR! The option '%s' needs a string argument.\n", option);    PrintHelp();    exit(1);  }  strcpy(ret, &argVect[*argNr][startChar]);  if (db4) printf("<<< GetStringOption '%s'\n", ret);  return TRUE;}/*****************************************************************************#*#  FUNCTION NAME: GetNumberOption*#*#  PARAMETERS: *#*#  DESCRIPTION:*#*#---------------------------------------------------------------------------*#  HISTORY*#*#  DATE     NAME     CHANGES*#  ----     ----     -------*#  960930   ronny    Initial version*#*#***************************************************************************/intGetNumberOption(int *argNr, int argCount, char *argVect[], unsigned int *ret,		char *option, int base){  int startChar;#if 0  startChar = strlen(option);  if (db4) printf(">>> GetNumberOption %s\r\n", argVect[*argNr]);  if (strlen(argVect[*argNr]) <= (unsigned int) startChar) {    if (db4) printf("Skipping to next arg...\r\n");    (*argNr)++;    startChar = 0;  }#else  (*argNr)++;  startChar = 0;#endif  if ((*argNr >= argCount)) {    printf("ERROR! The option '%s' needs a number argument.\n", option);    PrintHelp();    exit(1);  }  /*  *ret = strtol(&argVect[*argNr][startChar], (char **)NULL, 0);*/  if (base == 16) {    sscanf(&argVect[*argNr][startChar], "%x", ret);  } else {    *ret = atoi(&argVect[*argNr][startChar]);  }  if (db4) printf("<<< GetNumberOption %x\r\n", *ret);  return TRUE;}/*****************************************************************************#*#  FUNCTION NAME: PrintHelp*#*#  PARAMETERS: None.*#*#  DESCRIPTION: Prints help info.*#*#---------------------------------------------------------------------------*#  DATE     NAME     CHANGES*#  ----     ----     -------*#  960909   ronny    Initial version*#*#***************************************************************************/voidPrintHelp(){  printf("e100boot   [--device devicename] [--file filename addr [size]]* \n"         "           [--flash ram-source flash-destination size]*\n"         "           [--pause iter]*\n"         "           [--memtest addr addr]*\n"         "           [--memclear addr addr]* [--memdump addr addr]*\n"         "           [--setreg addr val]* [--getreg addr]* \n"         "           [--verify addr val]*\n"         "           [--5400]* [--5600]* [--testcard]* [--devboard]* \n"         "           [--bootfile file] [--jump addr]\n"         "           [--help]\n"         "\n"         "Boot Etrax100 over the network.\n"         "\n"         "Options:\n"	 "\n"	 "--help     Just print this message and exit.\n"	 "--device   Which device to send packets on. Default eth0.\n"	 "--file     The file to load and the adress to load it to. If file is \n"	 "           loaded on stdin, specify filename '-' followed by a size.\n"	 "--getreg   Print value of memory location. Must be uncached adress.\n"	 "--jump     Jump to specified adress.\n"	 "--flash    Flashes the specified RAM area to the Flash.\n"	 "--memclear Clears the specified memory area.\n"	 "--memdump  Prints the contents of the specified memory area.\n"	 "--memtest  Does a test of the specified memory area.\n"         "--pause    How many iterations to do of an empty loop.\n"	 "--setreg   Load dword to memory location. Must be uncached adress.\n"	 "--verify   Verify that memory contains dword. If not loading will stop\n"	 "--bootfile Which boot image to send to Etrax100. \n"         "           The following internal images are available:\n");  {    const struct boot_image_info_type *info;    int image_nbr = 0;    info = &boot_image_info[image_nbr];    while (info->name != NULL) {      printf("           %-10.10s - %s\n", info->name, info->info);      image_nbr++;      info = &boot_image_info[image_nbr];    }  }printf("--5400     Sets R_WAITSTATES, R_DRAM_TIMING and R_DRAM_CONFIG for the 5400 printserver. \n""--5600     Sets R_WAITSTATES, R_DRAM_TIMING and R_DRAM_CONFIG for the 5600 printserver. \n""--testcard Sets R_WAITSTATES, R_DRAM_TIMING and R_DRAM_CONFIG for the testcard.\n""--devboard Sets registers for the developer board.\n""\n""The options are done in the order specified on the command line,\n""so normally the --jump should be last.\n""\nAll addresses and sizes should be in hex with optional '0x' prefix.\n\n""Examples:\n""If you have a stripped binary (file.ima) linked to 0x08000000 that you want\n""to boot via the network, do this:\n""e100boot --file file.ima 88000000 --jump 08000000\n""\n""Or something like this. Sets waitstates to zero and loads two files,\n""the first from stdin:\n""cat file.ima | e100boot --memtest 88000000 8801ffff --memclear 88000000 8801ffff \n""--setreg b0000000 0 --getreg b0000000 --file - 88000000 a000 \n""--file file2.ima 88010000 --memdump 88000000 880000ff --jump 08000000\n""\n""Note that the files must be loaded to an uncached address, as the \n""bootloader lies in the cache. If you access any uncached address during boot,\n""the bootloader will be destroyed.\n");}/*****************************************************************************#*#  FUNCTION NAME: GetLocalEthAddr*#*#  PARAMETERS: None.*#*#  DESCRIPTION: *#*#---------------------------------------------------------------------------*#  DATE     NAME     CHANGES*#  ----     ----     -------*#  980818   ronny    Initial version*#*#***************************************************************************/#if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)/* * The *BSD version of this function isn't as trivial the the Linux one. * Ethernet devices are named based on the card (like ed0) and do not * have unified name as in Linux (ethX). * * So, we have to go through a interface table and search for a * ethernet device. * * NOTE: The use of K&R style is intentional, basically because of * two reasons, (1) I can't read the strange GNU style and (2) I * don't like it. If this is a problem, please re-indent! :-) /tobba */voidGetLocalEthAddr(void){	struct sockaddr_dl *sdl;	struct if_msghdr *ifm, *n_ifm;	struct ifa_msghdr *ifam;	size_t mem;	int mib[6] = {CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0};	int flags, n = 0, i = 0, ethaddrcnt = 0, addrcnt = 0;	char *buf, *lim, *next, *cp = NULL;		if (sysctl(mib, 6, NULL, &mem, NULL, 0) < 9) {		perror("iflist-sysctl failed");		exit(EXIT_FAILURE);	}		if ((buf = malloc(mem)) == NULL) {		perror("malloc");		exit(EXIT_FAILURE);	}		/* Get the interface table. */	if(sysctl(mib, 6, buf, &mem, NULL, 0) < 0) {		perror("error getting interface table");		exit(EXIT_FAILURE);	}		lim = buf + mem;	next = buf;		while (next < lim) {		ifm = (struct if_msghdr *) next;				if (ifm->ifm_type == RTM_IFINFO) {			sdl = (struct sockaddr_dl *) (ifm + 1);			flags = ifm->ifm_flags;		}		else {			fprintf(stderr, "error parsing NET_RT_IFLIST, expected: %d, got: %d\n",				RTM_IFINFO, ifm->ifm_type);			fprintf(stderr, "msglen: %d\n", ifm->ifm_msglen);			fprintf(stderr, "buf: %s, next: %s, lim: %s\n", buf, next, lim);			exit(EXIT_FAILURE);		}				next += ifm->ifm_msglen;		ifam = NULL;		addrcnt = 0;				while (next < lim) {			n_ifm = (struct if_msghdr *) next;			if (n_ifm->ifm_type != RTM_NEWADDR)				break;			if (ifam == NULL)				ifam = (struct ifa_msghdr *) n_ifm;						addrcnt++;			next += n_ifm->ifm_msglen;		}				/* Found interface, if its type is ethernet, save the MAC. */		if (sdl->sdl_type == IFT_ETHER) {			ethaddrcnt++;			strncpy(device, sdl->sdl_data, sdl->sdl_nlen);			device[sdl->sdl_nlen] = '\0';						cp = (char *) LLADDR(sdl);			if ((n = sdl->sdl_alen) > 0) {				while (--n >= 0 && i != 6)					eth_addr_local[i++] = *cp++ & 0xff;				if (db1)					printf("Ethernet address for device is: %s"					       "%02x:%02x:%02x:%02x:%02x:%02x\n",					       device,					       eth_addr_local[0],					       eth_addr_local[1],					       eth_addr_local[2],					       eth_addr_local[3],					       eth_addr_local[4],					       eth_addr_local[5]);			}		}	}	free(buf);}#elsevoidGetLocalEthAddr(){  int fd;  struct ifreq ifr;  if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {    perror("socket");    exit(1);  }  strcpy(ifr.ifr_name, device);  if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {    perror("ioctl");    exit(1);  }  memcpy(eth_addr_local, ifr.ifr_hwaddr.sa_data, 6);  if (db1) printf("Ethernet adress for device %s is %2.2x-%2.2x-%2.2x-%2.2x-%2.2x-%2.2x\n", 		  device,		  eth_addr_local[0],		  eth_addr_local[1],		  eth_addr_local[2],		  eth_addr_local[3],		  eth_addr_local[4],		  eth_addr_local[5]);  shutdown(fd, 2);}#endif /* __*BSD__ *//*****************************************************************************#*#  FUNCTION NAME: SendToDevice*#*#  PARAMETERS: *#*#  DESCRIPTION: *#*#---------------------------------------------------------------------------*#  DATE     NAME     CHANGES*#  ----     ----     -------*#  980818   ronny    Initial version*#*#***************************************************************************/voidSendToDevice(unsigned char *data, int data_len){  char *buf;  int padded_len = data_len;  char fName[256];  FILE *fd;  struct packet_header_T *h = (struct packet_header_T*) data;  D();    if (db1) printf("***> SendToDevice\n");  if (padded_len < 64) {    printf("Packet too small, padding to 64 bytes.\n");    padded_len = 64;  }  buf = (char*)calloc(1, padded_len);  memcpy(buf, data, data_len);  if (db2) printf("Sending %d bytes at 0x%x to %s.\n", padded_len, (unsigned int)buf, device);  if (db1) printf("#TX######################################################\n");  if (db1) DecodeSvintoBoot(buf);  if (db1) printf("#########################################################\n");  if (toFiles) {    sprintf(fName, "e100boot.seq%lu", (unsigned long)ntohl(h->seq));    if (db2) printf("Writing packet to file '%s'.\n", fName);    if ((fd = fopen(fName, "w+")) == NULL) {      printf("Cannot open/create '%s'. %s.\n", fName, strerror(errno));      exit(1);    }    fwrite(buf, padded_len, 1, fd);    fclose(fd);  }// Added by Andrey - to write to a single file  else if (singlefd) fwrite(buf, padded_len, 1, singlefd);  else if (sendto(sock_fd, buf, padded_len, 0, &sock_addr, sizeof(sock_addr)) < 0) {    perror("Sendto failed:");    exit(-1);  }  free(buf);  /*  PrintPacket(data, data_len, HEX);*/  if (db1) printf("<*** SendToDevice\n");}/*****************************************************************************#*#  FUNCTION NAME: InitSendSocket*#*#  PARAMETERS: *#*#  DESCRIPTION: *#*#---------------------------------------------------------------------------*#  DATE     NAME     CHANGES*#  ----     ----     -------*#  980818   ronny    Initial version*#***************************************************************************/#if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -