static int fill_buffer(stream_t * s, char * buffer, int max_len)
{
    int retry_cnt = 0;
#ifdef __LINUX__
    int r = read(s->fd, buffer, max_len);
#else
    int r = 0;
 if(fp_is_timeshift_file() == 1)
    ufs_read_func(s, buffer, max_len, &r);
 else
 	{
 	if( file_size>0)
 		{
 		u64 u_pos = 0;
 		ufs_tell(s->fd,&u_pos);
		if(u_pos>=file_size)
			r = 0;
			else
				ufs_read(s->fd, buffer, max_len, &r);
 		}
	else
 	ufs_read(s->fd, buffer, max_len, &r);
 	}
#endif

    // We are certain this is EOF, do not retry
    if (max_len && r == 0) {
        s->eof = 1;
    }

    return (r <= 0) ? -1 : r;
}
void
load_file_ino(ino32_t ino, const char *fn, uintptr_t loadadr, unsigned interactive, int part)
	/* fn:		 for message only */
{
	union ufs_dinode dinode;
	size_t sz;
	struct loadinfo inf;
	char buf[32];

	if (ino == 0 || ufs_get_inode(ino, &dinode))
		return;		/* not found */

	print(str_loading);		/* "loading " */
	print(fn);
	print(str_at);			/* " at 0x" */
	print(hexstr(buf, loadadr));
	print(str_dddot);		/* "..." */

	sz = DI_SIZE(&dinode);
	ufs_read(&dinode, (void *) loadadr, 0, sz);

	print(str_done);		/* "done\r\n" */

	/* digest executable format */
	inf.sec_size = sz;
	inf.entry_offset = 0;
	if (xi_load(&inf, (void *) loadadr)) {
		print(fn);
		print(str_ukfmt); /* ": unknown format -- exec from top\r\n" */
	}

	/* pass control to the secondary boot */
	dispatch(interactive, loadadr, loadadr + inf.sec_size, part,
	    loadadr + inf.entry_offset);
}
/*
 * look-up fn in directory dirino
 */
ino32_t
ufs_lookup(ino32_t dirino, const char *fn)
{
	union ufs_dinode dirdi;
	struct direct *pdir;
	char *p, *endp;
	size_t disize;

	if (ufs_get_inode(dirino, &dirdi))
		return 0;

	if ((dirdi.di_common.di_mode & IFMT) != IFDIR)
		return 0;			/* Not a directory */

	disize = DI_SIZE(&dirdi);

	p = alloca((disize + fs.bsize - 1) & ~(fs.bsize - 1));
	ufs_read(&dirdi, p, 0, disize);
	endp = p + disize;
	for ( ; pdir = (void *) p, p < endp; p += pdir->d_reclen) {
		if (pdir->d_ino && !strcmp(fn, pdir->d_name))
			return pdir->d_ino;
	}
	return 0;				/* No such file or directory */
}
Exemple #4
0
/*
 * Function: mmc_read
 * Arg     : Data address on card, o/p buffer & data length
 * Return  : 0 on Success, non zero on failure
 * Flow    : Read data from the card to out
 */
uint32_t mmc_read(uint64_t data_addr, uint32_t *out, uint32_t data_len)
{
	uint32_t ret = 0;
	uint32_t block_size;
	uint32_t read_size = SDHCI_ADMA_MAX_TRANS_SZ;
	void *dev;
	uint8_t *sptr = (uint8_t *)out;

	dev = target_mmc_device();
	block_size = mmc_get_device_blocksize();

	ASSERT(!(data_addr % block_size));
	ASSERT(!(data_len % block_size));

	/*
	 * dma onto write back memory is unsafe/nonportable,
	 * but callers to this routine normally provide
	 * write back buffers. Invalidate cache
	 * before read data from mmc.
         */
	arch_clean_invalidate_cache_range((addr_t)(out), data_len);

	if (target_mmc_device())
	{
		/* TODO: This function is aware of max data that can be
		 * tranferred using sdhci adma mode, need to have a cleaner
		 * implementation to keep this function independent of sdhci
		 * limitations
		 */
		while (data_len > read_size) {
			ret = mmc_sdhci_read((struct mmc_device *)dev, (void *)sptr, (data_addr / block_size), (read_size / block_size));
			if (ret)
			{
				dprintf(CRITICAL, "Failed Reading block @ %x\n",(unsigned int) (data_addr / block_size));
				return ret;
			}
			sptr += read_size;
			data_addr += read_size;
			data_len -= read_size;
		}

		if (data_len)
			ret = mmc_sdhci_read((struct mmc_device *)dev, (void *)sptr, (data_addr / block_size), (data_len / block_size));

		if (ret)
			dprintf(CRITICAL, "Failed Reading block @ %x\n",(unsigned int) (data_addr / block_size));
	}
	else
	{
		ret = ufs_read((struct ufs_dev *) dev, data_addr, (addr_t)out, (data_len / block_size));
		if (ret)
		{
			dprintf(CRITICAL, "Error: UFS read failed writing to block: %llu\n", data_addr);
		}

		arch_invalidate_cache_range((addr_t)out, data_len);
	}

	return ret;
}
static  int ufs_read_retry(stream_t * s, void * p_buff, u32 btr, u32 * p_br)
{
    int ret = FR_OK;
	mtos_sem_take(&ts_sem, 0);
    ret = ufs_read(s->fd, p_buff, btr, p_br);
mtos_sem_give(&ts_sem);
    if (ret != FR_OK) {
        return ret;
    }

    if (*p_br == 0) {
        //mtos_printk("\n%s %d\n", __func__, __LINE__);
        ret = open_ts_file(s);

        if (ret != FR_OK) {
            free(s->fd);
            s->fd = 0;
            s->eof = 1;
            mtos_printk("UFS OPEN FAIL %d\n", ret);
            return ret;
        }

        //  recv_bytes_inter = recv_bytes;// % mp_timeshift_byte;
        //mtos_printk("\n%s %d %d\n", __func__, __LINE__,(int)recv_bytes_inter);
        ret =  ufs_lseek_mode(s, (s64) recv_bytes_inter, 0);

        if (ret != FR_OK) {
            mtos_printk("UFS OPEN FAIL %d\n", ret);
            return ret;
        }
mtos_sem_take(&ts_sem, 0);
        ret = ufs_read(s->fd, p_buff, btr, p_br);
		mtos_sem_give(&ts_sem);
//mtos_printk("\n%s %d %d\n", __func__, __LINE__,*p_br);
        if (ret != FR_OK) {
            mtos_printk("UFS OPEN FAIL %d\n", ret);
            return ret;
        }
    }

    return ret;
}
Exemple #6
0
size_t
UfsFile::read(void *buf, size_t bytes, off_t ofs)
{
	size_t sz;

	if (ofs != -1)
		ufs_seek(_f, ofs, SEEK_SET);
	ufs_read(_f, buf, bytes, &sz);

	return bytes - sz;
}
size_t
ufs_load_file(void *buf, ino32_t dirino, const char *fn)
{
	size_t cnt, disize;
	union ufs_dinode dinode;

	if (ufs_fn_inode(dirino, fn, &dinode))
		return (unsigned) 0;
	disize = DI_SIZE(&dinode);
	cnt = ufs_read(&dinode, buf, 0, disize);

	return cnt;
}
void
ufs_list_dir(ino32_t dirino)
{
	union ufs_dinode dirdi;
	struct direct *pdir;
	char *p, *endp;
	size_t disize;

	if (ufs_get_inode(dirino, &dirdi))
		errx(1, "ino = %d: not found", dirino);

	disize = DI_SIZE(&dirdi);
	p = alloca((disize + fs.bsize - 1) & ~(fs.bsize - 1));
	ufs_read(&dirdi, p, 0, disize);
	endp = p + disize;
	for ( ; pdir = (void *) p, p < endp; p += pdir->d_reclen) {
		if (pdir->d_ino)
			printf("%6d %s\n", pdir->d_ino, pdir->d_name);
	}
}
Exemple #9
0
static long loadprog(long *hsize)
{
	long addr;	/* physical address.. not directly useable */
	long hmaddress;
	unsigned long pad;
	long i;
	static int (*x_entry)() = 0;

	ufs_read(&head, (long) sizeof(head));
	if (N_BADMAG(head)) {
		printf("Invalid format!\n");
		exit(0);
	}

	startaddr = (long)head.a_entry;
	addr = (startaddr & 0x00ffffffl); /* some MEG boundary */
	printf("Booting @ 0x%lx\n", addr);
	if(addr < 0x100000l)
	{
		printf("Start address too low!\n");
		exit(0);
	}

	poff = N_TXTOFF(head)+head.a_text+head.a_data+head.a_syms;
	ufs_read((void *)&i, sizeof(long));
	*hsize = head.a_text+head.a_data+head.a_bss;
	*hsize = (*hsize+NBPG-1)&~(NBPG-1);
	*hsize += i+4+head.a_syms;
	addr=hmaddress=get_high_memory(*hsize);
	if (!hmaddress) {
		printf("Sorry, can't allocate enough memory!\n");
		exit(0);
	}

	poff = N_TXTOFF(head);

	/********************************************************/
	/* LOAD THE TEXT SEGMENT                                */
	/********************************************************/
	printf("text=0x%lx ", head.a_text);
	xread(addr, head.a_text);
	addr += head.a_text;

	/********************************************************/
	/* Load the Initialised data after the text		*/
	/********************************************************/
	while (addr & CLOFSET)
		pm_copy("\0", addr++, 1);

	printf("data=0x%lx ", head.a_data);
	xread(addr, head.a_data);
	addr += head.a_data;

	/********************************************************/
	/* Skip over the uninitialised data			*/
	/* (but clear it)					*/
	/********************************************************/
	printf("bss=0x%lx ", head.a_bss);
	pbzero(addr, head.a_bss);
	addr += head.a_bss;

	/* Pad to a page boundary. */
	pad = (unsigned long)(addr-hmaddress+(startaddr & 0x00ffffffl)) % NBPG;
	if (pad != 0) {
		pad = NBPG - pad;
		addr += pad;
	}
	bootinfo.bi_symtab = addr-hmaddress+(startaddr & 0x00ffffffl);

	/********************************************************/
	/* Copy the symbol table size				*/
	/********************************************************/
	pm_copy((char *)&head.a_syms, addr, sizeof(head.a_syms));
	addr += sizeof(head.a_syms);

	/********************************************************/
	/* Load the symbol table				*/
	/********************************************************/
	printf("symbols=[+0x%lx+0x%lx+0x%lx", pad, (long) sizeof(head.a_syms),
	       (long) head.a_syms);
	xread(addr, head.a_syms);
	addr += head.a_syms;

	/********************************************************/
	/* Load the string table size				*/
	/********************************************************/
	ufs_read((void *)&i, sizeof(long));
	pm_copy((char *)&i, addr, sizeof(long));
	i -= sizeof(long);
	addr += sizeof(long);

	/********************************************************/
	/* Load the string table				*/
	/********************************************************/
	printf("+0x%x+0x%lx] ", sizeof(long), i);
	xread(addr, i);
	addr += i;

	bootinfo.bi_esymtab = addr-hmaddress+(startaddr & 0x00ffffffl);

	/*
	 * For backwards compatibility, use the previously-unused adaptor
	 * and controller bitfields to hold the slice number.
	 */
	printf("total=0x%lx entry point=0x%lx\n",
		addr-hmaddress+(startaddr & 0x00ffffffl),
		startaddr & 0x00ffffffl);

	return hmaddress;
}
Exemple #10
0
/*
 * Get inode from disk.
 */
int
get_lfs_inode(ino32_t ino, union ufs_dinode *dibuf)
{
	struct ufs_info *ufsinfo = &ufs_info;
	daddr_t daddr;
	char *buf = alloca(fsi.bsize);
	struct ulfs1_dinode *di, *diend;
	int i;

	/* Get fs block which contains the specified inode. */
	if (ino == LFS_IFILE_INUM)
		daddr = fsi_lfs.idaddr;
	else {
#ifdef DEBUG_WITH_STDIO
		printf("LFS: ino: %d\nifpb: %d, bsize: %d\n",
			ino, fsi_lfs.ifpb, fsi.bsize);
#endif
		ufs_read((union ufs_dinode *) &ifile_dinode, buf,
			 ino / fsi_lfs.ifpb + fsi_lfs.ioffset,
			 fsi.bsize);
		i = ino % fsi_lfs.ifpb;
		daddr = (fsi_lfs.version == 1) ?
		    ((IFILE_V1 *) buf + i)->if_daddr
		    : ((IFILE *) buf + i)->if_daddr;
	}
#ifdef DEBUG_WITH_STDIO
	printf("LFS(%d): daddr: %d\n", ino, (int) daddr);
#endif

	if (daddr == LFS_UNUSED_DADDR)
		return 1;

	/* Read the inode block. */
	RAW_READ(buf, daddr << fsi.fsbtodb, fsi_lfs.ibsize);

	/* Search for the inode. */
	di = (struct ulfs1_dinode *) buf;
	diend = di + fsi_lfs.inopb;

	for ( ; di < diend; di++)
		if (di->di_inumber == ino)
			goto found;
	/* not found */
	return 1;

found:
#ifdef DEBUG_WITH_STDIO
	printf("LFS: dinode(%d): mode 0%o, nlink %d, inumber %d, size %d, uid %d, db[0] %d\n",
		ino, di->di_mode, di->di_nlink, di->di_inumber,
		(int) di->di_size, di->di_uid, di->di_db[0]);
#endif

#if 0	/* currently UFS1 only */
#if defined(USE_UFS1) && defined(USE_UFS2)
	/* XXX for DI_SIZE() macro */
	if (ufsinfo->ufstype != UFSTYPE_UFS1)
		di->di1.di_size = di->si2.di_size;
#endif
#endif

	dibuf->dil1 = *di;

	return 0;
}
AMRAudioFileSource*
AMRAudioFileSource::createNew(UsageEnvironment& env, char const* fileName) {
#ifdef __LINUX__
  FILE* fid = NULL;
#else
  UFILE* fid = NULL;
#endif
  Boolean magicNumberOK = True;
  do {

    fid = OpenInputFile(env, fileName);
    if (fid == NULL) break;

    // Now, having opened the input file, read the first few bytes, to
    // check the required 'magic number':
    magicNumberOK = False; // until we learn otherwise
    Boolean isWideband = False; // by default
    unsigned numChannels = 1; // by default
    char buf[100];
    // Start with the first 6 bytes (the first 5 of which must be "#!AMR"):
#ifdef __LINUX__
    if (fread(buf, 1, 6, fid) < 6) break;
#else
    u32 read_size = 0;
    int ret = 0;
    ret = ufs_read(fid, buf, 6, &read_size);
    if ((read_size != 6) || (ret != FR_OK)) break;
#endif
    if (strncmp(buf, "#!AMR", 5) != 0) break; // bad magic #
    unsigned bytesRead = 6;

    // The next bytes must be "\n", "-WB\n", "_MC1.0\n", or "-WB_MC1.0\n"
    if (buf[5] == '-') {
      // The next bytes must be "WB\n" or "WB_MC1.0\n"
#ifdef __LINUX__
      if (fread(&buf[bytesRead], 1, 3, fid) < 3) break;
#else
      u32 read_size = 0;
      int ret = 0;
      ret = ufs_read(fid, &buf[bytesRead], 3, &read_size);
      if ((read_size != 3) || (ret != FR_OK)) break;
#endif
      if (strncmp(&buf[bytesRead], "WB", 2) != 0) break; // bad magic #
      isWideband = True;
      bytesRead += 3;
    }
    if (buf[bytesRead-1] == '_') {
      // The next bytes must be "MC1.0\n"
#ifdef __LINUX__
      if (fread(&buf[bytesRead], 1, 6, fid) < 6) break;
#else
      u32 read_size = 0;
      int ret = 0;
      ret = ufs_read(fid, &buf[bytesRead], 6, &read_size);
      if ((read_size != 6) || (ret != FR_OK)) break;
#endif
      if (strncmp(&buf[bytesRead], "MC1.0\n", 6) != 0) break; // bad magic #
      bytesRead += 6;

      // The next 4 bytes contain the number of channels:
      char channelDesc[4];
#ifdef __LINUX__
      if (fread(channelDesc, 1, 4, fid) < 4) break;
#else
      ret = ufs_read(fid, channelDesc, 4, &read_size);
      if ((read_size != 4) || (ret != FR_OK)) break;
#endif
      numChannels = channelDesc[3]&0xF;
    } else if (buf[bytesRead-1] != '\n') {
      break; // bad magic #
    }

    // If we get here, the magic number was OK:
    magicNumberOK = True;

#ifdef DEBUG
    fprintf(stderr, "isWideband: %d, numChannels: %d\n",
	    isWideband, numChannels);
#endif
    return new AMRAudioFileSource(env, fid, isWideband, numChannels);
  } while (0);

  // An error occurred:
  CloseInputFile(fid);
  if (!magicNumberOK) {
    env.setResultMsg("Bad (or nonexistent) AMR file header");
  }
  return NULL;
}
// Note: We should change the following to use asynchronous file reading, #####
// as we now do with ByteStreamFileSource. #####
void AMRAudioFileSource::doGetNextFrame() {
#ifdef __LINUX__
  if (feof(fFid) || ferror(fFid))
#else
  if (1)    //change by jiakun, should change for feof and ferror func based on ufs file system
#endif
  {
    handleClosure(this);
    return;
  }

  // Begin by reading the 1-byte frame header (and checking it for validity)
  while (1) {
#ifdef __LINUX__
    if (fread(&fLastFrameHeader, 1, 1, fFid) < 1)
#else
    u32 read_size;
    int ret;
    ret = ufs_read(fFid, (void *)&fLastFrameHeader, 1, &read_size);
    if ((read_size != 1) || (ret != FR_OK))
#endif
    {
      handleClosure(this);
      return;
    }
    if ((fLastFrameHeader&0x83) != 0) {
#ifdef DEBUG
      fprintf(stderr, "Invalid frame header 0x%02x (padding bits (0x83) are not zero)\n", fLastFrameHeader);
#endif
    } else {
      unsigned char ft = (fLastFrameHeader&0x78)>>3;
      fFrameSize = fIsWideband ? frameSizeWideband[ft] : frameSize[ft];
      if (fFrameSize == FT_INVALID) {
#ifdef DEBUG
	fprintf(stderr, "Invalid FT field %d (from frame header 0x%02x)\n",
		ft, fLastFrameHeader);
#endif
      } else {
	// The frame header is OK
#ifdef DEBUG
	fprintf(stderr, "Valid frame header 0x%02x -> ft %d -> frame size %d\n", fLastFrameHeader, ft, fFrameSize);
#endif
	break;
      }
    }
  }

  // Next, read the frame-block into the buffer provided:
  fFrameSize *= fNumChannels; // because multiple channels make up a frame-block
  if (fFrameSize > fMaxSize) {
    fNumTruncatedBytes = fFrameSize - fMaxSize;
    fFrameSize = fMaxSize;
  }
#ifdef __LINUX__
  fFrameSize = fread(fTo, 1, fFrameSize, fFid);
#else
  u32 read_size;
  int ret;
  ret = ufs_read(fFid, (void *)fTo, fFrameSize, &read_size);
  fFrameSize = (unsigned)read_size;
#endif

  // Set the 'presentation time':
  if (fPresentationTime.tv_sec == 0 && fPresentationTime.tv_usec == 0) {
    // This is the first frame, so use the current time:
#ifdef __LINUX__    
   // gettimeofday(&fPresentationTime, NULL);
    //gettimeofday(&fPresentationTime, NULL);//peacer del
    gettimeofday_replace(&fPresentationTime, NULL);

#else
    gettimeofday_replace(&fPresentationTime, NULL);
#endif
  } else {
    // Increment by the play time of the previous frame (20 ms)
    unsigned uSeconds	= fPresentationTime.tv_usec + 20000;
    fPresentationTime.tv_sec += uSeconds/1000000;
    fPresentationTime.tv_usec = uSeconds%1000000;
  }

  fDurationInMicroseconds = 20000; // each frame is 20 ms

  // Switch to another task, and inform the reader that he has data:
  nextTask() = envir().taskScheduler().scheduleDelayedTask(0,
				(TaskFunc*)FramedSource::afterGetting, this);
 }
static int open_f(stream_t * stream, int mode, void * opts, int * file_format)
{
#ifndef __LINUX__
    ufs_file_t * f = malloc(sizeof(ufs_file_t)); //= stream->fd;
    recv_bytes = 0;
    memset(f, 0, sizeof(ufs_file_t));
    memset(filename1, 0, 512);
#else
    int f = 0;
#endif
    mode_t m = 0;
    file_size = 0;
    long long len;
    unsigned char * filename = NULL;
    struct stream_priv_s * p = (struct stream_priv_s *)opts;

    if (mode == STREAM_READ) {
        m = O_RDONLY;
    } else if (mode == STREAM_WRITE) {
        m = O_RDWR | O_CREAT | O_TRUNC;
    } else {
        mp_msg(MSGT_OPEN, MSGL_ERR, "[file] Unknown open mode %d\n", mode);
        m_struct_free(&stream_opts, opts);
        return STREAM_UNSUPPORTED;
    }

    if (p->filename) {
        filename = p->filename;
    } else if (p->filename2) {
        filename = p->filename2;
    } else {
        filename = NULL;
    }

    if (!filename) {
        mp_msg(MSGT_OPEN, MSGL_ERR, "[file] No filename\n");
        m_struct_free(&stream_opts, opts);
        return STREAM_ERROR;
    }

#if HAVE_DOS_PATHS

    // extract '/' from '/x:/path'
    if (filename[ 0 ] == '/' && filename[ 1 ] && filename[ 2 ] == ':') {
        filename++;
    }

#endif
    m |= O_BINARY;

    if (!strcmp(filename, "-")) {
#ifdef __LINUX__

        if (mode == STREAM_READ) {
            // read from stdin
            mp_msg(MSGT_OPEN, MSGL_INFO, MSGTR_ReadSTDIN);
            f = 0; // 0=stdin
#if HAVE_SETMODE
            setmode(fileno(stdin), O_BINARY);
#endif
        } else {
            mp_msg(MSGT_OPEN, MSGL_INFO, "Writing to stdout\n");
            f = 1;
#if HAVE_SETMODE
            setmode(fileno(stdout), O_BINARY);
#endif
        }

#endif
    } else {
#ifdef __LINUX__
        mode_t openmode = S_IRUSR | S_IWUSR;
#ifndef __MINGW32__
        openmode |= S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
#endif
        f = open(filename, m, openmode);

        if (f < 0) {
            mp_msg(MSGT_OPEN, MSGL_ERR, MSGTR_FileNotFound, filename);
            m_struct_free(&stream_opts, opts);
            return STREAM_ERROR;
        }

#else
        //  ufs_asc2uni(filename,filename1);
        //printf("\n before ufs open filename:%s \n",filename);
        memcpy(filename1, filename, strlen(filename));
        int ret  = 0;
        unsigned short * p_filename1;
        {
            unsigned short  path_tmp[512] = {0};
            p_filename1 = Convert_Utf8_To_Unicode((unsigned char *)filename, path_tmp);
            ret = ufs_open(f, p_filename1, UFS_READ);
        }

        if (ret != FR_OK) {
            mtos_printk("[%s][ERROR][ERROR]   UFS OPEN FAIL %d!!!!!!!!!\n", __func__, ret);
            m_struct_free(&stream_opts, opts);
            free(f);
            return STREAM_ERROR;
        }

#if 1
        // mtos_printk("\n%s %d \n", __func__, __LINE__);
        {
            int r = 0;
            char pvr_timeshift[512] = {0};
            char pvr_ists[32] = {0};
            recv_bytes_inter = 0;
            ret = ufs_read(f, pvr_timeshift, 512, &r);
            int time_mode;
            mp_timeshift_size = 0;
            file_size = 0;
			sect_size = 0;
            if (ret != FR_OK) {
                mtos_printk("[%s][ERROR][ERROR]   UFS OPEN FAIL %d!!!!!!!!!\n", __func__, ret);
                m_struct_free(&stream_opts, opts);
                return STREAM_ERROR;
            }

            if (strstr(pvr_timeshift, "pvrtimeshift")) {
                memcpy(&mp_timeshift_size, pvr_timeshift + 32, sizeof(int));
                memcpy(&time_mode, pvr_timeshift + 32 + sizeof(int), sizeof(int));
                memcpy(basename, pvr_timeshift + 32 + 2 * sizeof(int), 256);
                memcpy(&file_size, pvr_timeshift + 384, sizeof(int));
		  memcpy(&sect_size, pvr_timeshift + 384+sizeof(int), sizeof(int));
                rec_index = 1;
            }
        }
#endif
        printf("\n  ufs open ok f: %x \n", f);
#ifndef __LINUX__
        ret = ufs_ioctl(f, FS_SEEK_LINKMAP, 0 , 0 , 0);
#endif
#endif
    }

#ifdef __LINUX__
    len = lseek(f, 0, SEEK_END);
    lseek(f, 0, SEEK_SET);
#else

    if (fp_is_timeshift_file() == 1) {
		while(mp_rec_bytes<sect_size*2)
			{
			mtos_task_sleep(100);
			}
        len = mp_rec_bytes;
    } else if (file_size > 0) {
        len = file_size;
    } else {
        len = f->file_size;
    }

    if (ufs_lseek(f, 0, 0) == FR_OK) {
        OS_PRINTF("#########@@@@#######\n");
    } else {
        OS_PRINTF("#########@@1111@@#######\n");
    }

#endif
#ifdef __MINGW32__

    // seeks on stdin incorrectly succeed on MinGW
    if (f == 0) {
        len = -1;
    }

#endif

    if (len == -1) {
        if (mode == STREAM_READ) {
            stream->seek = seek_forward;
        }

        stream->type = STREAMTYPE_STREAM; // Must be move to STREAMTYPE_FILE
        stream->flags |= MP_STREAM_SEEK_FW;
    } else if (len >= 0) {
        stream->seek = seek;
        stream->end_pos = len;
        stream->type = STREAMTYPE_FILE;
    }

    mp_msg(MSGT_OPEN, MSGL_V, "[file] File size is %"PRId64" bytes\n", (int64_t)len);
    //  #ifdef __LINUX__
    stream->fd = f;
    //#endif
    stream->fill_buffer = fill_buffer;
    stream->write_buffer = write_buffer;
    stream->control = control;
    stream->read_chunk = 64 * 1024;
    m_struct_free(&stream_opts, opts);
    return STREAM_OK;
}
static  int ufs_read_func(stream_t * s, void * p_buff, u32 btr, u32 * p_br)
{
    int ret = 0;
    int retry_cnt = 0;
    u64 mp_timeshift_byte = ((u64)mp_timeshift_size) * 1024 ;
    int mp_rec_bytes_inter;
    //if(fp_is_timeshift_file()== 0)
    {
        if (s->fd != 0) {
            wait_for_rec_ready();
			 mtos_sem_take(&ts_sem, 0);
            ret = ufs_read(s->fd, p_buff, btr, p_br);
			 mtos_sem_give(&ts_sem);

            if (ret != FR_OK) {
                if (!((ret == FR_DISK_ERR) && ((*p_br) > 0))) {
                    mtos_printk("UFS OPEN FAIL %d\n", ret);
                   // return 0;
                }
            }
        } else {
            return -1;
        }
    }

    while ((*p_br) == 0 && fp_is_timeshift_file() == 1 && (is_file_seq_exit() == FALSE)) {
        unsigned short  path_tmp[256] = {0};
        unsigned short * p_filename1;
        int file_len = 256;

        //if(recv_bytes>()mp_timeshift_size)
        // mtos_task_sleep(1000);
        if (recv_bytes_inter + btr <= mp_timeshift_byte) {
            ret = ufs_read_retry(s, p_buff, btr, p_br);

            if (ret != FR_OK) {
                if (!((ret == FR_DISK_ERR) && ((*p_br) > 0))) {
                    mtos_printk("UFS OPEN FAIL %d\n", ret);
                    //return 0;
                }
            }
        } else {
            int r_modify = 0;
            int read_len_sec = btr - (int)(mp_timeshift_byte - recv_bytes_inter);

            if (mp_timeshift_byte - recv_bytes_inter > 0) {
                ret =   ufs_read_retry(s, p_buff, (int)(mp_timeshift_byte - recv_bytes_inter), p_br);

                if (ret != FR_OK) {
                    if (!((ret == FR_DISK_ERR) && ((*p_br) > 0))) {
                        mtos_printk("UFS OPEN FAIL %d\n", ret);
                        return 0;
                    }
                }
            }

            {
                char file_new[256] = {0};
                rec_index ++;
                ret = open_ts_file(s);

                if (ret != FR_OK) {
                    free(s->fd);
                    s->fd = 0;
                    s->eof = 1;
                    mtos_printk("UFS OPEN FAIL %d\n", ret);
                    break;
                }

                ret = ufs_lseek_mode(s, 1024, 0);

                if (ret != FR_OK) {
                    free(s->fd);
                    s->fd = 0;
                    s->eof = 1;
                    mtos_printk("UFS OPEN FAIL %d\n", ret);
                    break;
                }
            }
mtos_sem_take(&ts_sem, 0);
            ret = ufs_read(s->fd, p_buff + (int)(mp_timeshift_byte - recv_bytes_inter), read_len_sec, &r_modify);
			mtos_sem_give(&ts_sem);
            recv_bytes+= 1024;

            //mtos_printk("\n%s %d\n",__func__,__LINE__);
            if (ret != FR_OK) {
                if (!((ret == FR_DISK_ERR) && (r_modify > 0))) {
                    // mtos_printk("UFS OPEN FAIL %d\n", ret);
                    return 0;
                }
            }

            (*p_br) += r_modify;
        }

        //mtos_printk("\n%s %d \n", __func__, __LINE__);
        retry_cnt++;

        //mtos_printk("\n%s %d \n", __func__, __LINE__);
        if (retry_cnt > 50) {
            break;
        }

        //mtos_printk("\n%s %d \n", __func__, __LINE__);
    }
	//if(recv_bytes_inter>1024*1024*1000)
	 //  mtos_printk("\n%s %d %d\n",__func__,__LINE__,(int)recv_bytes_inter);
	if((recv_bytes_inter + (*p_br)== mp_timeshift_byte)&& fp_is_timeshift_file() == 1)
				{
				{
                char file_new[256] = {0};
                rec_index ++;
                ret = open_ts_file(s);

                if (ret != FR_OK) {
                    free(s->fd);
                    s->fd = 0;
                    s->eof = 1;
                    return ret;
                }

                ret = ufs_lseek_mode(s, 1024, 0);

                if (ret != FR_OK) {
                    free(s->fd);
                    s->fd = 0;
                    s->eof = 1;
                     return ret;
                }
            }
			  recv_bytes+= 1024;

     
				}
    recv_bytes += (*p_br);
    recv_bytes_inter  = recv_bytes%mp_timeshift_byte;
}