void writeBinaryFile(libusb_device_handle *handler,
		     unsigned char cmd,
		     unsigned int sector,
		     const char *filename,
		     unsigned int sector2,
		     const unsigned char *flags) {
  FILE *file1=NULL;
  struct stat stat1;
  unsigned int len=0;
  int r1;
  
  memset (&stat1,0,sizeof(stat1));
  
  file1 = fopen(filename,"rb");
  if (file1 == NULL) {
    error_at_line(0,0,__FILE__,__LINE__,"Error: cannot read file %s",filename);
    return;
  }
  r1 = fstat(fileno(file1), &stat1);
  fclose(file1);
  file1 = NULL;
  if (r1 != 0) {
    error_at_line(0,0,__FILE__,__LINE__,"Error: cannot read file stat %s",filename);
    return;
  }
  len = (unsigned int)stat1.st_size;
  writeBinaryFileSeek(handler,cmd,sector,filename,0,len,sector2,flags);
  
}
Пример #2
0
static bool
format_check (const lex_pos_ty *pos, void *msgid_descr, void *msgstr_descr,
	      bool equality, bool noisy, const char *pretty_msgstr)
{
  struct spec *spec1 = (struct spec *) msgid_descr;
  struct spec *spec2 = (struct spec *) msgstr_descr;
  bool err = false;

  if (spec1->named_arg_count + spec2->named_arg_count > 0)
    {
      unsigned int i, j;
      unsigned int n1 = spec1->named_arg_count;
      unsigned int n2 = spec2->named_arg_count;

      /* Check the argument names are the same.
	 Both arrays are sorted.  We search for the first difference.  */
      for (i = 0, j = 0; i < n1 || j < n2; )
	{
	  int cmp = (i >= n1 ? 1 :
		     j >= n2 ? -1 :
		     strcmp (spec1->named[i].name, spec2->named[j].name));

	  if (cmp > 0)
	    {
	      if (noisy)
		{
		  error_with_progname = false;
		  error_at_line (0, 0, pos->file_name, pos->line_number,
				 _("a format specification for argument '%s', as in '%s', doesn't exist in 'msgid'"),
				 spec2->named[j].name, pretty_msgstr);
		  error_with_progname = true;
		}
	      err = true;
	      break;
	    }
	  else if (cmp < 0)
	    {
	      if (equality)
		{
		  if (noisy)
		    {
		      error_with_progname = false;
		      error_at_line (0, 0, pos->file_name, pos->line_number,
				     _("a format specification for argument '%s' doesn't exist in '%s'"),
				     spec1->named[i].name, pretty_msgstr);
		      error_with_progname = true;
		    }
		  err = true;
		  break;
		}
	      else
		i++;
	    }
	  else
	    j++, i++;
	}
    }

  return err;
}
Пример #3
0
/*
 * This function is an /enumerator/ in the terminology of iteratees
 * [http://okmij.org/ftp/Streams.html].
 *
 * Return value: 0 - success, -1 - error.
 */
static int
process_file(enum Codec_T ct, const char *inpath, struct Buffer *inbuf,
             const struct Repr_Format *repr)
{
    debug_print("process_file: \"%s\"", inpath);
    FILE *f = NULL;

    if (streq(inpath, "-")) {
        f = stdin;
    } else if ((f = fopen(inpath, "rb")) == NULL) {
        error(0, errno, "%s", inpath);
        return -1;
    }

    if (adjust_buffer(inbuf, f) < 0) {
        error(0, errno, "%s", inpath);
        return -1;
    }

    int retval = -1;
    size_t filepos = 0;
    struct Stream str = STREAM_INIT;
    void *z = NULL;

    for (;;) {
        const size_t orig_size = read_block(inbuf->wptr, inbuf->size,
                                            f, &str);
        str.type = ((str.size = orig_size) == 0) ? S_EOF : S_CHUNK;
        str.data = inbuf->wptr;

        if (str.type == S_EOF && str.errmsg != NULL) {
            error_at_line(0, 0, inpath, filepos, "%s", str.errmsg);
            break;
        }

        const IterV indic = run_codec(ct, &z, &str, repr);
        assert(indic == IE_DONE || indic == IE_CONT);
        filepos += orig_size - str.size;

        if (indic == IE_CONT && str.errmsg != NULL) {
            error_at_line(0, 0, inpath, filepos, "%s", str.errmsg);
            break;
        }

        assert(str.size == 0);

        if (indic == IE_DONE) {
            retval = 0;
            break;
        }
    }

    if (!streq(inpath, "-"))
        retval |= fclose(f);

    free(str.errmsg);
    free_codec(ct, z); /* XXX malloc/free for each input file is not good */
    return retval;
}
Пример #4
0
static void
init_inotify (void)
{
  ino_fd = inotify_init ();
  if (ino_fd < 0)
    error_at_line (1, errno, __FILE__, __LINE__, "Failed to init inotify");

  int wd = inotify_add_watch (ino_fd, watch_dir, IN_CREATE);
  if (wd < 0)
    error_at_line (1, errno, __FILE__, __LINE__,
                   "Failed to add a watch for %s", watch_dir);
}
Пример #5
0
int io_read_fixed_length (
    int fd,
    PEGASUS_CMPIR_IO_BUFPTR_TYPE * buf,
    size_t count )
{
    ssize_t bytes;

    while (count > 0)
    {
        //invokes read on unix and recv on windows systems
        bytes = PEGASUS_CMPIR_RECV ( fd, (char *)buf, count,0 );
        if (bytes == 0)
        {
            error_at_line (
                0,
                0,
                __FILE__,
                __LINE__,
                "EOF before reading complete "
                "chunk of data from fd: %d",
                fd);
            return -1;
        }
        else if (bytes == -1)
        {
            if (errno != EINTR && errno != EAGAIN)
            {
                error_at_line (
                    0,
                    errno,
                    __FILE__,
                    __LINE__,
                    "could not read all the "
                    "requested data from fd: %d",
                    fd);
                return -1;
            }
        }
        else
        {
            count -= bytes;
#ifndef PEGASUS_OS_ZOS
            buf   += bytes;
#else
            buf   = (void*) ((long) buf + (long) bytes);
#endif
        }
    }
    return 0;
}
Пример #6
0
/*!
  The function tries to load "lib<id>.so" looking for an entry point
  "<id>_InitCommLayer". The latter one is called to obtain a provider_comm
  structure including the function pointer table for MI calls towards
  remote providers.

  \param id the name of the comm-layer for which the library has to be loaded.
  \param broker broker handle as passed to the init function.
  \param ctx context as passed to the init function.

  \return pointer to the provider_comm structure from the comm-layer, or NULL.
 */
static provider_comm * load_comm_library ( const char * id,
					   CONST CMPIBroker * broker,
					   CONST CMPIContext * ctx )
{
	void * hLibrary;
    CMPIStatus rc = {CMPI_RC_OK, NULL};

	TRACE_VERBOSE(("entered function."));
	TRACE_NORMAL(("loading comm-layer library: lib%s.so", id));

	hLibrary = _load_lib ( id );

	if ( hLibrary != NULL ) {
		char function[255];
		INIT_COMM_LAYER fp;
		sprintf ( function, "%s_InitCommLayer", id );
#ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
		fp = (INIT_COMM_LAYER) dllqueryfn ( (dllhandle *) hLibrary, function );
#else
		fp = (INIT_COMM_LAYER) dlsym ( hLibrary, function );
#endif

		if ( fp != NULL ) {
			provider_comm * result = fp ( broker, ctx );
			result->id = strdup ( id );
			result->handle = hLibrary;

			TRACE_INFO(("comm-layer successfully initialized."));
			TRACE_VERBOSE(("leaving function."));
			return result;
		}
#ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
		dllfree ( (dllhandle*) hLibrary );
#else
		dlclose ( hLibrary );
#endif
	}
#ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
	error_at_line ( 0, errno, __FILE__, __LINE__,
			"Unable to load/init communication-layer library.");
#else
	error_at_line ( 0, 0, __FILE__, __LINE__,
			"Unable to load/init communication-layer library: %s",
			dlerror () );
#endif
	TRACE_VERBOSE(("leaving function."));
	return NULL;
}
Пример #7
0
void KmpMatcher(char *T, char *P)
{
	int i, m, l;
	int *Pi;
	l = strlen(T);
	Pi = malloc(l * sizeof(int));
	if (Pi == NULL) {
		error_at_line(0, (int) NULL, __FILE__, __LINE__, "%s",
			      "Out of space!!!");
	}
	ComputePrefixFunction(T, Pi);
//    printf("Pi: \n");
//    for (i = 0; i < l; i++) {
//        printf("%d ", Pi[i]);
//    }
//    printf("\n");

	m = 0;
	for (i = 0; P[i] != '\0'; i++) {
		while (m > 0 && P[i] != T[m]) {
			m = Pi[m - 1];
		}
		if (P[i] == T[m]) {
			m++;
		}
		if (m == l) {
			printf("Pattern occurs with shift %d.\n",
			       i + 1 - l);
			m = Pi[m - 1];
		}
	}

	free(Pi);
	Pi = NULL;
}
int writeBootloaderBin(libusb_device_handle *handler, const char *filename) {
  int i, found = 0;

  struct bootloader_info boot_info;
  memset (&boot_info, 0, sizeof(boot_info));

  if (get_bootloader_info(filename, &boot_info) != 0) {
    return -1;
  }

  for (i = 0; i < 10; i++) {
    if (!strncmp(boot_info.part[i].name, "bootloader.ini", 13)) {
      found = 1;
      break;
    }
  }

  if (!found) {
    error_at_line(0,0,__FILE__,__LINE__, "Error: faild to find para");
    return -1;
  }

  printf ("info: name(%s) addr(0x%x) offset(0x%x) size(%d)\n",
	  boot_info.part[i].name,
	  boot_info.part[i].addr,
	  boot_info.part[i].offset << 9,
	  boot_info.part[i].size << 9);

  writeBinaryFileSeek(handler, '\x05', 0xe406e000, filename,
		      boot_info.part[i].offset << 9,
		      boot_info.part[i].size << 9, 0, NULL);
  return 0;
}
Пример #9
0
/* Parse the mode to interpret the given coordinates. */
void *
ui_parse_coordinate_mode(struct argp_option *option, char *arg,
                         char *filename, size_t lineno, void *junk)
{
  char *outstr;

  /* We want to print the stored values. */
  if(lineno==-1)
    {
      gal_checkset_allocate_copy( *(int *)(option->value)==IMGCROP_MODE_IMG
                                  ? "img" : "wcs", &outstr );
      return outstr;
    }
  else
    {
      if      (!strcmp(arg, "img")) *(int *)(option->value)=IMGCROP_MODE_IMG;
      else if (!strcmp(arg, "wcs")) *(int *)(option->value)=IMGCROP_MODE_WCS;
      else
        error_at_line(EXIT_FAILURE, 0, filename, lineno, "`%s' (value to "
                      "`--mode') not recognized as an input mode. "
                      "Recognized values are `img' and `wcs'. This option "
                      "is necessary to identify the nature of your input "
                      "coordinates.\n\n"
                      "Please run the following command for more "
                      "information (press the `SPACE' key to go down and "
                      "`q' to return to the command-line):\n\n"
                      "    $ info gnuastro \"Crop modes\"\n", arg);
      return NULL;
    }
}
static bool
format_check (const lex_pos_ty *pos, void *msgid_descr, void *msgstr_descr,
	      bool equality, bool noisy, const char *pretty_msgstr)
{
  struct spec *spec1 = (struct spec *) msgid_descr;
  struct spec *spec2 = (struct spec *) msgstr_descr;
  bool err = false;
  unsigned int i;

  for (i = 0; i < spec1->arg_count || i < spec2->arg_count; i++)
    {
      bool arg_used1 = (i < spec1->arg_count && spec1->args_used[i]);
      bool arg_used2 = (i < spec2->arg_count && spec2->args_used[i]);

      /* The translator cannot omit a %n from the msgstr because that would
	 yield a "Argument missing" warning at runtime.  */
      if (arg_used1 != arg_used2)
	{
	  if (noisy)
	    {
	      error_with_progname = false;
	      error_at_line (0, 0, pos->file_name, pos->line_number,
			     arg_used1
			     ? _("a format specification for argument %u doesn't exist in '%s'")
			     : _("a format specification for argument %u, as in '%s', doesn't exist in 'msgid'"),
			     i, pretty_msgstr);
	      error_with_progname = true;
	    }
	  err = true;
	  break;
	}
    }

  return err;
}
void b96_uninit_device(libusb_device_handle *handler) {
  int r1;
  r1 = libusb_release_interface(handler, 0);
  if (r1 != 0) {
    error_at_line(0,0,__FILE__,__LINE__,"Error: cannot release device interface");
  }
  libusb_close(handler);
}
void unknownCMD07(libusb_device_handle *handler) {
  unsigned char data[1024];
  int transferred;
  int r1;
  int i;
  int len=0;

  memset(data,0,sizeof(data));
  /* signature */
  data[0] = '\x55';
  data[1] = '\x53';
  data[2] = '\x42';
  data[3] = '\x43';
  /* tag */
  data[4] = '\0';
  data[5] = '\0';
  data[6] = '\0';
  data[7] = '\0';
  /* Data Transfer Length */
  data[8] = len%256;
  data[9] = (len/256)%256;
  data[10] = (len/256/256)%256;
  data[11] = (len/256/256/256)%256;
  /* flags */
  data[12] = '\x00';
  /* lun */
  data[13] = '\x00';
  /* cdb length: 00 */
  data[14] = '\x00';

  /* cbwcb */
  /* cbwcb - scsi cmd */
  data[15] = '\x10';
  /* sector ? */
  data[16] = '\x00';
  data[17] = '\xf0';
  data[18] = '\x06';
  data[19] = '\xe4';
  /* length ? */
  data[20] = len%256;
  data[21] = (len/256)%256;
  data[22] = (len/256/256)%256;
  data[23] = (len/256/256/256)%256;

  printf ("CBW:");
  for (i=0; i<31; i++) {
    printf (" %02x", data[i]);
  }
  printf ("\n");
  transferred=0;
  r1 = libusb_bulk_transfer(handler, 0x01, data, 31, &transferred, 0);
  if (r1 != 0) {
    error_at_line(0,0,__FILE__,__LINE__,"Error: cannot send CBW for read");
  }
  printf ("Transffered: %d\n",transferred);

  readCSW(handler);
}
int get_bootloader_info(const char *filename, struct bootloader_info *boot_info) {
  int ret;
  FILE *fp = NULL;
  unsigned char buf[704];
  int i;


  fp = fopen(filename, "rb");
  if (fp == NULL) {
    error_at_line(0,0,__FILE__,__LINE__, "Error: Cannot open %s", filename);
    return -1;
  }

  ret = fseek(fp, BOOTLOADER_PACK_STRUCT_OFFSET, SEEK_SET);
  if (ret < 0) {
    error_at_line(0,0,__FILE__,__LINE__, "Error: seek err");
    return -1;
  }

  memset (boot_info, 0, sizeof(struct bootloader_info));
  ret = fread(buf, 1, 704, fp);
  if (ret != sizeof(struct bootloader_info)) {
    error_at_line(0,0,__FILE__,__LINE__, "Error: read pack info err");
    return -1;
  }
  memcpy(boot_info->resv, buf, 64);
  for (i=0; i<10; i++) {
    memcpy(boot_info->part[i].name, &(buf[64+i*64]), 16);
    boot_info->part[i].size = (((unsigned int)(buf[64+i*64+16])) & 0x00ff)
      + ((((unsigned int)(buf[64+i*64+17])) & 0x00ff) << 8)
      + ((((unsigned int)(buf[64+i*64+18])) & 0x00ff) << 16)
      + ((((unsigned int)(buf[64+i*64+19])) & 0x00ff) << 24);
    boot_info->part[i].offset = (((unsigned int)(buf[64+i*64+20])) & 0x00ff)
      + ((((unsigned int)(buf[64+i*64+21])) & 0x00ff) << 8)
      + ((((unsigned int)(buf[64+i*64+22])) & 0x00ff) << 16)
      + ((((unsigned int)(buf[64+i*64+23])) & 0x00ff) << 24);
    boot_info->part[i].addr = (((unsigned int)(buf[64+i*64+24])) & 0x00ff)
      + ((((unsigned int)(buf[64+i*64+25])) & 0x00ff) << 8)
      + ((((unsigned int)(buf[64+i*64+26])) & 0x00ff) << 16)
      + ((((unsigned int)(buf[64+i*64+27])) & 0x00ff) << 24);
    memcpy(boot_info->part[i].resv, &(buf[64+i*64+28]), 36);
  }
  fclose(fp);
  return 0;
}
Пример #14
0
mat_t *mat_dup(const mat_t *m){
	mat_t *m2 = (mat_t*)malloc(sizeof(mat_t));
	if(m2){
		memcpy(m2,m,sizeof(mat_t));
	}else{
		error_at_line(1,0,__FILE__,__LINE__,"not enough memory\n");
	}
	return m2;
}
Пример #15
0
mat_t *mat_new_zero(void){
	mat_t *m = (mat_t*)malloc(sizeof(mat_t));
	if(m){
		memset(m,0,sizeof(mat_t));
	}else{
		error_at_line(1,0,__FILE__,__LINE__,"not enough memory\n");
	}
	return m;
}
Пример #16
0
//Use to send all the messages Msg, even the TRAIN ones, but in fact, TRAIN messages will never be created for the sending, but use only on reception... Thus, to send TRAIN messages, sendTrain will be used.
//use globalAddrArray defined in management_addr.h
int sendOther(address addr, bool isPred, MType type, address sender){
  int length;
  int iovcnt = 1;
  struct iovec iov[1];
  int rank = -1;
  trComm * aComm;
  int result;
  Msg * msg;

  if (type == TRAIN) {
    error_at_line(EXIT_FAILURE, errno, __FILE__, __LINE__,
        "Wrong MType given to sendOther");
    return (-1);
  } else {
    msg = malloc(sizeof(newMsg(type, sender)));
    *msg = newMsg(type, sender);

    length = msg->len;
    rank = addrToRank(addr);
    if (rank != -1) {
      aComm = getTComm(rank, isPred, globalAddrArray);
      if (aComm == NULL ) {
        free(msg);
        return (-1);
      }
      //printf("Send message = %s on comm %p\n", msgTypeToStr(type), aComm);
      iov[0].iov_base = msg;
      iov[0].iov_len = length;
      result = commWritev(aComm, iov, iovcnt);
      if (result != length)
        fprintf(stderr, "result!=length\n");
      free(msg);
      return (result);
    } else {
      //should return an error if the addr is out of rank
      free(msg);
      error_at_line(EXIT_FAILURE, errno, __FILE__, __LINE__,
          "Sending failure in sendOther (addr = %d)", addr);
      return (-1);    //same error as commWritev !!
    }
  }
}
static bool
format_check (const lex_pos_ty *pos, void *msgid_descr, void *msgstr_descr,
	      bool equality, bool noisy, const char *pretty_msgstr)
{
  struct spec *spec1 = (struct spec *) msgid_descr;
  struct spec *spec2 = (struct spec *) msgstr_descr;
  bool err = false;
  unsigned int i;

  /* Check the argument types are the same.  */
  if (equality
      ? spec1->unnumbered_arg_count != spec2->unnumbered_arg_count
      : spec1->unnumbered_arg_count < spec2->unnumbered_arg_count)
    {
      if (noisy)
	{
	  error_with_progname = false;
	  error_at_line (0, 0, pos->file_name, pos->line_number,
			 _("number of format specifications in 'msgid' and '%s' does not match"),
			 pretty_msgstr);
	  error_with_progname = true;
	}
      err = true;
    }
  else
    for (i = 0; i < spec2->unnumbered_arg_count; i++)
      if (spec1->unnumbered[i].type != spec2->unnumbered[i].type)
	{
	  if (noisy)
	    {
	      error_with_progname = false;
	      error_at_line (0, 0, pos->file_name, pos->line_number,
			     _("format specifications in 'msgid' and '%s' for argument %u are not the same"),
			     pretty_msgstr, i + 1);
	      error_with_progname = true;
	    }
	  err = true;
	}

  return err;
}
Пример #18
0
/// @brief 服务化函数
/// @param[in] cmd 应用程序名
/// @param[in] option,facility openlog参数
/// @retval 0 成功 -1错误
int daemonize(const char *cmd,int option,int facility)
{

	/// 清除mask
	umask(0);
	/// 关闭所有文件描述符
	struct rlimit rl;
	if(getrlimit(RLIMIT_NOFILE,&rl)<0)
	{
		error_at_line(0,errno,__FILE__,__LINE__,"%s:cannot get file limit",cmd);
		return -1;
	}
	if(rl.rlim_max==RLIM_INFINITY)
		rl.rlim_max=1024;
	for(int i=0;i<rl.rlim_max;i++)
		close(i);
	/// 成为会话首进程
	pid_t pid;
	if((pid=fork())<0)
	{
		error_at_line(0,errno,__FILE__,__LINE__,"%s: cannot fork",cmd);
		return -1;
	}
	if(pid!=0)
		exit(0);
	setsid();
	/// 改变工作目录
	if(chdir("/")<0)
	{
		error_at_line(0,errno,__FILE__,__LINE__,"%s: chdir error",cmd);
		return -1;
	}
	/// 绑定0,1,2描述为/dev/null
	int fd0,fd1,fd2;
	fd0=open("/dev/null",O_RDWR);
	fd1=dup(0);
	fd2=dup(1);
	/// 打开日志
	openlog(cmd,option,facility);
	return 0;
}
static bool
format_check (const lex_pos_ty *pos, void *msgid_descr, void *msgstr_descr,
	      bool equality, bool noisy, const char *pretty_msgstr)
{
  struct spec *spec1 = (struct spec *) msgid_descr;
  struct spec *spec2 = (struct spec *) msgstr_descr;
  bool err = false;

  if (spec1->named_arg_count + spec2->named_arg_count > 0)
    {
      unsigned int i, j;
      unsigned int n1 = spec1->named_arg_count;
      unsigned int n2 = spec2->named_arg_count;

      /* Check the argument names in spec1 are contained in those of spec2.
	 Additional arguments in spec2 are allowed; they expand to themselves
	 (including the surrounding braces) at runtime.
	 Both arrays are sorted.  We search for the differences.  */
      for (i = 0, j = 0; i < n1 || j < n2; )
	{
	  int cmp = (i >= n1 ? 1 :
		     j >= n2 ? -1 :
		     strcmp (spec1->named[i].name, spec2->named[j].name));

	  if (cmp > 0)
	    j++;
	  else if (cmp < 0)
	    {
	      if (equality)
		{
		  if (noisy)
		    {
		      error_with_progname = false;
		      error_at_line (0, 0, pos->file_name, pos->line_number,
				     _("a format specification for argument '%s' doesn't exist in '%s'"),
				     spec1->named[i].name, pretty_msgstr);
		      error_with_progname = true;
		    }
		  err = true;
		  break;
		}
	      else
		i++;
	    }
	  else
	    j++, i++;
	}
    }

  return err;
}
void resetBulkOnlyMassStorage(libusb_device_handle *handler) {
  int r1;
  r1 = libusb_control_transfer(handler,
			  LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
			  '\xff',
			  0,
			  1,
			  NULL,
			  0,
			  1000);
  if (r1 != 0) {
    error_at_line(0,0,__FILE__,__LINE__,"Error: cannot reset device usbms");
  }
}
char* find_firmware(const char *filename, char *firmwareFilename, int firmwareFilenameSize) {
  char *listOfDataDirectory[] = { NULL, "/usr/local/share/linaro-adfu-tool/bg96/firmwares", "/usr/share/linaro-adfu-tool/bg96/firmwares", "./firmwares", "../firmwares" };
  char dataDirectory[4096];
  int i;
  struct stat fileStat;

  listOfDataDirectory[0] = dataDirectory;

  if (getbindirectory(dataDirectory,sizeof(dataDirectory))==NULL) {
    error_at_line(0,0,__FILE__,__LINE__,"Cannot get executable directory");
    dataDirectory[0]='.';
    dataDirectory[1]='\0';
  } else {
    strncat(dataDirectory,"../firmwares",sizeof(dataDirectory));
  }

  for (i=0; i<sizeof(listOfDataDirectory)/sizeof(listOfDataDirectory[0]); i++) {
    int r1;
    memset(&fileStat,0,sizeof(fileStat));
    snprintf(firmwareFilename, firmwareFilenameSize, "%s/%s", listOfDataDirectory[i], filename);
    r1 = stat(firmwareFilename, &fileStat);
    if (r1 != 0) {
      continue;
    }
    if ( (!S_ISREG(fileStat.st_mode))
	 && (!S_ISLNK(fileStat.st_mode))
	 && (!S_ISFIFO(fileStat.st_mode))
	 ) {
      continue;
    }
    printf ("Use %s for %s\n", firmwareFilename, filename);
    return firmwareFilename;
  }
  error_at_line(0,0,__FILE__,__LINE__, "Cannot find %s", filename);
  memset(firmwareFilename,0,firmwareFilenameSize);
  return NULL;
}
Пример #22
0
Файл: core.c Проект: antrik/kgi
void setmode(void)
{
	kgi_mode_t *mode;
	kgi_error_t err;

	mode=malloc(sizeof (kgi_mode_t));
	if(!mode)
		error_at_line(1, errno, __FILE__, __LINE__, "setmode()");
	memset(mode, 0, sizeof (kgi_mode_t));

	mode->revision=KGI_MODE_REVISION;

	mode->dev_mode=malloc(display->mode_size);
	if(!mode->dev_mode)
		error_at_line(1, errno, __FILE__, __LINE__, "setmode()");

	mode->images=1;

	mode->img[0].fam |= KGI_AM_COLORS;
	mode->img[0].bpfa[0] = 5;
	mode->img[0].bpfa[1] = 6;
	mode->img[0].bpfa[2] = 5;
	mode->img[0].bpfa[3] = 0;
	mode->img[0].frames = 1;
	mode->img[0].size.x = 640;
	mode->img[0].size.y = 480;
	mode->img[0].virt.x = 640;
	mode->img[0].virt.y = 480;

	err=(display->CheckMode)(display, KGI_TC_PROPOSE, mode->img, mode->images, mode->dev_mode, mode->resource, __KGI_MAX_NR_RESOURCES);
	if(err)
		error(4, 0, "setmode(): (display->CheckMode)() failed.");

	(display->SetMode)(display, mode->img, mode->images, mode->dev_mode);

	display->mode = mode;
}
Пример #23
0
mat_t *mat_new(	float xx, float yx, float zx, float wx,
		float xy, float yy, float zy, float wy,
		float xz, float yz, float zz, float wz,
		float xw, float yw, float zw, float ww){
	mat_t *m = (mat_t*)malloc(sizeof(mat_t));
	if(m){
		m->x = vec_new(xx,yx,zx,wx);
		m->y = vec_new(xy,yy,zy,wy);
		m->z = vec_new(xz,yz,zz,wz); 
		m->w = vec_new(xw,yw,zw,ww);
	}else{
		error_at_line(1,0,__FILE__,__LINE__,"not enough memory\n");
	}
	return m;
}
Пример #24
0
/*!
    The function tries to load "lib<id>.so" looking for an entry point
    "<id>_InitCommLayer". The latter one is called to obtain a provider_comm
    structure including the function pointer table for MI calls towards
    remote providers.

    \param id the name of the comm-layer for which the library has to be loaded.
    \param broker broker handle as passed to the init function.
    \param ctx context as passed to the init function.

    \return pointer to the provider_comm structure from the comm-layer, or NULL.
*/
static provider_comm * load_comm_library (
    const char * id,
    CONST CMPIBroker * broker,
    CONST CMPIContext * ctx )
{
    void * hLibrary;
    char function[255];
    CMPIStatus rc = {CMPI_RC_OK, NULL};

    TRACE_VERBOSE(("entered function."));
    TRACE_NORMAL(("loading comm-layer library: lib%s.so", id));

    hLibrary = _load_lib ( id );

    if (hLibrary != NULL)
    {
        INIT_COMM_LAYER fp;
        sprintf ( function, "%s_InitCommLayer", id );
        //invokes dlsym on unix and GetProcAddress on windows
        fp = (INIT_COMM_LAYER)PEGASUS_CMPIR_GETPROCADDRESS(hLibrary,function);
        if (fp != NULL)
        {
            provider_comm * result = fp(broker, ctx);
            result->id = strdup ( id );
            result->handle = hLibrary;

            TRACE_INFO(("comm-layer successfully initialized."));
            TRACE_VERBOSE(("leaving function."));
            return result;
        }
        //invokes dlfree call on unix and FreeLibrary on windows
        PEGASUS_CMPIR_FREELIBRARY( hLibrary );
    }
    error_at_line (
        0,
        errno,
        __FILE__,
        __LINE__,
        "Unable to load/init communication-layer library.%s Error",
        id);
    TRACE_VERBOSE(("leaving function."));
    return NULL;
}
Пример #25
0
static void
loop_inotify (void)
{
  int keep_going = 1;

  union {
    unsigned char        chars[INO_BUF_SIZE];
    struct inotify_event ev;
  } buf;

  while (keep_going) {
    if (read (ino_fd, &buf.chars, INO_BUF_SIZE) < 1) {
      error_at_line (1, 0, __FILE__, __LINE__,
                     "Inotify read buffer too small: %u", INO_BUF_SIZE);
    }

    if (buf.ev.len && buf.ev.mask == IN_CREATE &&
        ! strcmp (buf.ev.name, watch_file)) {
      keep_going = 0;
    }
  }
}
Пример #26
0
Файл: core.c Проект: antrik/kgi
kern_return_t kgi_check_mode(trivfs_protid_t io_object)
{
	assert(display);
	assert(display->CheckMode);

	if (!io_object)
		return EOPNOTSUPP;
	if (!(io_object->po->openmodes & O_WRITE))
		return EBADF;

	fprintf(stderr, "kgi_check_mode()\n");

	{
		struct po_state *const state = io_object->po->hook;
		kgi_mode_t *const mode = &state->mode;

		kgi_error_t err;

		/* first things first... */
		if (state->status != KGI_STATUS_NONE || !state->mode.images)
			return EPROTO;

		mode->dev_mode = malloc(display->mode_size);
		if(!mode->dev_mode)
			error_at_line(1, errno, __FILE__, __LINE__, "setmode()");

		err = (display->CheckMode)(display, KGI_TC_PROPOSE, mode->img, mode->images, mode->dev_mode, mode->resource, __KGI_MAX_NR_RESOURCES);
		if (err) {
			free(mode->dev_mode);
			return EINVAL;
		}

		state->status = KGI_STATUS_CHECKED;
	}

	return 0;
}
Пример #27
0
/* Print a message with 'vfprintf (stderr, FORMAT, ARGS)';
   if ERRNUM is nonzero, follow it with ": " and strerror (ERRNUM).
   If STATUS is nonzero, terminate the program with 'exit (STATUS)'.
   If FNAME is not NULL, prepend the message with "FNAME:LINENO:".
   Use the globals error_print_progname, error_message_count, and
   error_one_per_line similarly to error_at_line().  */
void
verror_at_line (int status, int errnum, const char *file,
                unsigned int line_number, const char *format, va_list args)
{
  char *message = xvasprintf (format, args);
  if (message)
    {
      /* Until http://sourceware.org/bugzilla/show_bug.cgi?id=2997 is fixed,
         glibc violates GNU Coding Standards when the file argument to
         error_at_line is NULL.  */
      if (file)
        error_at_line (status, errnum, file, line_number, "%s", message);
      else
        error (status, errnum, "%s", message);
    }
  else
    {
      /* EOVERFLOW, EINVAL, and EILSEQ from xvasprintf are signs of
         serious programmer errors.  */
      error (0, errno, _("unable to display error message"));
      abort ();
    }
  free (message);
}
void writeBinaryFileSeek(libusb_device_handle *handler,
			 unsigned char cmd,
			 unsigned int sector,
			 const char *filename,
			 unsigned int seek,
			 unsigned int len,
			 unsigned int sector2,
			 const unsigned char *flags) {
  FILE *file1=NULL;
  unsigned char data[1024];
  int transferred;
  int r1;
  static unsigned char buf1[2*1024*1024];
  int i;
  int tLen;

  memset (data,0,sizeof(data));
  
  file1 = fopen(filename,"rb");
  if (file1 == NULL) {
    error_at_line(0,0,__FILE__,__LINE__,"Error: cannot read file %s",filename);
    return;
  }
  fseek(file1,seek,SEEK_SET);

  /* signature */
  data[0] = '\x55';
  data[1] = '\x53';
  data[2] = '\x42';
  data[3] = '\x43';
  /* tag */
  data[4] = '\0';
  data[5] = '\0';
  data[6] = '\0';
  data[7] = '\0';
  /* Data Transfer Length */
  data[8] = len%256;
  data[9] = (len/256)%256;
  data[10] = (len/256/256)%256;
  data[11] = (len/256/256/256)%256;
  /* flags */
  data[12] = '\x00';
  /* lun */
  data[13] = '\x00';
  /* cdb length: 16 */
  data[14] = '\x10';

  /* cbwcb */
  /* cbwcb - scsi cmd */
  data[15] = cmd;
  /* sector ? */
  data[16] = sector%256;
  data[17] = (sector/256)%256;
  data[18] = (sector/256/256)%256;
  data[19] = (sector/256/256/256)%256;
  /* length ? */
  data[20] = len%256;
  data[21] = (len/256)%256;
  data[22] = (len/256/256)%256;
  data[23] = (len/256/256/256)%256;

  data[24] = sector2%256;
  data[25] = (sector2/256)%256;
  data[26] = (sector2/256/256)%256;
  data[27] = (sector2/256/256/256)%256;

  
  if (flags != NULL) {
    for (i=28; i<31; i++) {
      data[i] = flags[i-28];
    }
  }

  printf ("CBW:");
  for (i=0; i<31; i++) {
    printf (" %02x", data[i]);
  }
  printf ("\n");
  transferred=0;
  r1 = libusb_bulk_transfer(handler, 0x01, data, 31, &transferred, 0);
  if (r1 != 0) {
    error_at_line(0,0,__FILE__,__LINE__,"Error: cannot send CBW for read");
  }
  if (transferred != 31) {
    error_at_line(0,0,__FILE__,__LINE__,"Error: transferred %d != 31",transferred);
  }
  sleep(1);

  
  for (tLen = len; tLen > 0 && !feof(file1); ) {
    int rLen=0;
    if (tLen > sizeof(buf1)) {
      rLen = fread(buf1,1,sizeof(buf1),file1);
    } else {
      rLen = fread(buf1,1,tLen, file1);
    }
    if (rLen <= 0) {
      break;
    }
    transferred = 0;
    r1 = libusb_bulk_transfer(handler, 0x01, buf1, rLen, &transferred, 0);
    if (r1 != 0) {
      error_at_line(0,0,__FILE__,__LINE__,"Error: cannot send data");
      break;
    }
    if (transferred != rLen) {
      error_at_line(0,0,__FILE__,__LINE__,"Error: transferred %d != rLen = %d",transferred,rLen);
      break;
    }
    sleep(1);
    printf ("Bulk transferred %d bytes\n",rLen);
    tLen -= rLen;
  }
  
  fclose(file1);
  file1 = NULL;

  readCSW(handler);
}
libusb_device_handle* start(int argc, char **argv) {
  libusb_device_handle *handler = NULL;
  char firmwareFilename[4096];
  
  handler = b96_init_device();
  if (handler == NULL) {
    return NULL;
  }
  printf ("Handler: %p\n",handler);

  if (find_firmware("adfudec.bin", firmwareFilename, sizeof(firmwareFilename))==NULL) {
    error_at_line(0,0,__FILE__,__LINE__, "Error: Cannot find adfudec.bin");
    return handler;
  }
  writeBinaryFile(handler, '\x05', 0xe406f000u, firmwareFilename, 0, NULL);
  sleep(1);

  if (find_firmware("bootloader.bin", firmwareFilename, sizeof(firmwareFilename))==NULL) {
    error_at_line(0,0,__FILE__,__LINE__, "Error: Cannot find bootloader.bin");
    return handler;
  }
  writeBootloaderBin(handler, firmwareFilename);
  sleep(1);

  unknownCMD07(handler);
  sleep(10);

  libusb_close(handler);
  handler = b96_init_device();
  if (handler == NULL) {
    return NULL;
  }
  setuid(getuid());

  if (find_firmware("bl31.bin", firmwareFilename, sizeof(firmwareFilename))==NULL) {
    error_at_line(0,0,__FILE__,__LINE__, "Error: Cannot find bl31.bin");
    return handler;
  }
  /* load bl31.bin to 0x1f000000 */
  writeBinaryFile(handler, '\xcd' ,0x13, firmwareFilename,  0x1f000000u, NULL);

  if (find_firmware("bl32.bin", firmwareFilename, sizeof(firmwareFilename))==NULL) {
    error_at_line(0,0,__FILE__,__LINE__, "Error: Cannot find bl32.bin");
    return handler;
  }
  /* load bl32.bin to 0x1f202000 */
  writeBinaryFile(handler, '\xcd', 0x13, firmwareFilename, 0x1f202000u, NULL);

  /* load u-boot-dtb.img to 0x10ffffc0. (Note: u-boot is at 0x11000000,
     -0x40 is the header */
  writeBinaryFile(handler, '\xcd', 0x13, argv[1], 0x10ffffc0u, NULL);
  sleep(2);

  /* jump to 0x1f000000 (bl31.bin)*/
  unknownCMD50(handler, 0x1f000000u);
  sleep(2);

  libusb_close(handler);
  handler = NULL;
  return handler;
}
/**
 * Open the device and init it
 * You should call b96_init_usb() first before using this function
 *
 * @return the handler of the device
 */
libusb_device_handle* b96_init_device(void) {
  libusb_device_handle *handler=NULL;
  int r1;
  int c1;
  struct libusb_device_descriptor desc;
  int i;

  handler = libusb_open_device_with_vid_pid(libusb_ctx, 0x10d6, 0x10d6);
  if (handler == NULL) {
    error_at_line(0,0,__FILE__,__LINE__,"Error: cannot open device 10d6:10d6");
    return handler;
  }

  r1 = libusb_get_device_descriptor(libusb_get_device(handler), &desc);
  if (r1 != 0) {
    error_at_line(0,0,__FILE__,__LINE__,"Error: cannot get device descriptor");
  }
  printf ("bDescriptorType: %d\n", desc.bDescriptorType);
  printf ("bNumConfigurations: %d\n", desc.bNumConfigurations);
  printf ("iManufacturer: %d\n", desc.iManufacturer);

  for (i=1; i<=desc.bNumConfigurations; i++) {
    struct libusb_config_descriptor *config=NULL;
    int r2=0;
    int j;
    r2 = libusb_get_config_descriptor_by_value(libusb_get_device(handler), i, &config);
    if (r2 != 0) {
      error_at_line(0,0,__FILE__,__LINE__,"Error: cannot get configuration %d descriptor", i);
      continue;
    }
    printf ("bNumInterfaces: %d\n", config->bNumInterfaces);
    for (j=0; j<config->bNumInterfaces; j++) {
      
    }
    if (config != NULL) {
      libusb_free_config_descriptor(config);
      config=NULL;
    }
  }

  r1 = libusb_detach_kernel_driver(handler, 0);
  if (r1 != 0) {
    const char *errorStr="unknown";
    if (r1 == LIBUSB_ERROR_NOT_FOUND) {
      errorStr = "LIBUSB_ERROR_NOT_FOUND (no worry)";
    } else if (r1 == LIBUSB_ERROR_INVALID_PARAM) {
      errorStr = "LIBUSB_ERROR_INVALID_PARAM";
    } else if (r1 == LIBUSB_ERROR_NO_DEVICE) {
      errorStr = "LIBUSB_ERROR_NO_DEVICE";
    } else if (r1 == LIBUSB_ERROR_NOT_SUPPORTED) {
      errorStr = "LIBUSB_ERROR_NOT_SUPPORTED";
    }
    error_at_line(0,0,__FILE__,__LINE__,"Info: cannot detach kernel driver: %s", errorStr);
  }
  
  c1 = 0;
  r1 = libusb_get_configuration(handler,  &c1);
  if (r1 != 0) {
    error_at_line(0,0,__FILE__,__LINE__,"Error: cannot get device configuration");
  }
  printf ("Configuiration: %d\n",c1);
  r1 = libusb_set_configuration(handler, c1);
  if (r1 != 0) {
    error_at_line(0,0,__FILE__,__LINE__,"Error: cannot set device configuration");
  }

  r1 = libusb_claim_interface(handler, 0);
  if (r1 != 0) {
    error_at_line(0,0,__FILE__,__LINE__,"Error: cannot claim device interface");
  }
  
  return handler;
}