/*******************************************************************************
* Function Name  : MSD_ReadMultipleBlock
* Description    : None
* Input          : - sector:
*				   - buffer:
                   - count:扇区数
* Output         : None
* Return         : count  返回读剩余扇区数	0:OK 其它:ERROR
* Attention		 : None
*******************************************************************************/
int MSD_ReadMultipleBlock(uint32_t sector, uint8_t *buffer, uint8_t count)
{
    uint8_t r1;

    /* if ver = SD2.0 HC, sector need <<9 */
    if(CardInfo.CardType != CARDTYPE_SDV2HC)
    {
        sector = sector << 9;
    }
    /* Send CMD18 : Read multiple block command */
    r1 = _send_command(CMD18, sector, 0);

    if(r1 != 0x00)
    {
        return 1;
    }

    do
    {
        if (_read_buffer(buffer, MSD_BLOCKSIZE, HOLD)) break;
        buffer += 512;
    }
    while (--count);

    /* Send stop data transmit command - CMD12 */
    _send_command(CMD12, 0, 0);

    return count;
}
Beispiel #2
0
u8 *eid_get_entry(s8 *file, u64 entnum)
{
	u32 i, length;
	u8 *res = NULL;
	u8 *eid = _read_buffer(file, &length);

	if(eid != NULL)
	{
		eid_header_t *h = (eid_header_t *)eid;

		//Fix header.
		_es_eid_header(h);

		eid_entry_t *e = (eid_entry_t *)(eid + sizeof(eid_header_t));
		for(i = 0; i < h->entcnt; i++, e++)
		{
			//Fix entry.
			_es_eid_entry(e);

			if(e->entnum == entnum)
			{
				res = (u8 *)malloc(e->size);
				memcpy(res, eid + e->offset, e->size);
				break;
			}
		}

		free(eid);
	}
	else
		printf("error: could not read %s\n", file);

	return res;
}
/*******************************************************************************
* Function Name  : MSD_ReadSingleBlock
* Description    : None
* Input          : - sector:
*				   - buffer:
* Output         : None
* Return         : None
* Attention		 : None
*******************************************************************************/
int MSD_ReadSingleBlock(uint32_t sector, uint8_t *buffer)
{
    uint8_t r1;

    /* if ver = SD2.0 HC, sector need <<9 */
    if(CardInfo.CardType != CARDTYPE_SDV2HC)
    {
        sector = sector << 9;
    }

    /* Send CMD17 : Read single block command */
    r1 = _send_command(CMD17, sector, 0);

    if(r1 != 0x00)
    {
        return 1;
    }

    /* Start read and return the result */
    r1 = _read_buffer(buffer, MSD_BLOCKSIZE, RELEASE);

    /* Send stop data transmit command - CMD12 */
    _send_command(CMD12, 0, 0);

    return r1;
}
Beispiel #4
0
void eid_unpack(s8 *file)
{
	u32 i, length;
	u8 *eid = _read_buffer(file, &length);

	if(eid != NULL)
	{
		eid_header_t *h = (eid_header_t *)eid;

		//Fix header.
		_es_eid_header(h);

		eid_entry_t *e = (eid_entry_t *)(eid + sizeof(eid_header_t));
		for(i = 0; i < h->entcnt; i++, e++)
		{
			//Fix entry.
			_es_eid_entry(e);

			s8 fname[128];
			sprintf(fname, "%s%d", file, (u32)e->entnum);
			printf("writing entry @ offset 0x%08x (0x%08x bytes) to %s\n", e->offset, e->size, fname);
			_write_buffer(fname, eid + e->offset, e->size);
		}

		free(eid);
	}
	else
		printf("error: could not read %s\n", file);
}
Beispiel #5
0
void eid0_list_infos(s8 *file_in)
{
	u32 length;
	eid05_section_t *sptr;
	u8 *eid0 = _read_buffer(file_in, &length);

	if(eid0 != NULL)
	{
		printf("EID0:\n");

		u8 section_0[EID0_SECTION_0_SIZE];
		eid0_decrypt_section_0(eid0, section_0);
		_hexdump(stdout, "Section 0:", 0, section_0, EID0_SECTION_0_SIZE, TRUE);
		sptr = (eid05_section_t *)section_0;
		_hexdump(stdout, " Data:    ", 0, sptr->data, 0x60, FALSE);
		_hexdump(stdout, " Common:  ", 0, sptr->common, 0x30, FALSE);
		_hexdump(stdout, " Unk:     ", 0, sptr->unk, 0x18, FALSE);
		_hexdump(stdout, " OMAC:    ", 0, sptr->omac, 0x10, FALSE);
		_hexdump(stdout, " Pad:     ", 0, sptr->pad, 0x08, FALSE);

		u8 section_A[EID0_SECTION_A_SIZE];
		eid0_decrypt_section_A(eid0, section_A);
		_hexdump(stdout, "Section A:", 0, section_A, EID0_SECTION_A_SIZE, TRUE);
		sptr = (eid05_section_t *)section_A;
		_hexdump(stdout, " Data:    ", 0, sptr->data, 0x60, FALSE);
		_hexdump(stdout, " Common:  ", 0, sptr->common, 0x30, FALSE);
		_hexdump(stdout, " Unk:     ", 0, sptr->unk, 0x18, FALSE);
		_hexdump(stdout, " OMAC:    ", 0, sptr->omac, 0x10, FALSE);
		_hexdump(stdout, " Pad:     ", 0, sptr->pad, 0x08, FALSE);

		free(eid0);
	}
}
Beispiel #6
0
void eid4_decrypt(s8 *file_in, s8 *file_out)
{
	u32 length;
	u8 *eid4 = _read_buffer(file_in, &length);

	if(eid4 != NULL)
	{
		eid4_decrypt_buffer(eid4);
		_write_buffer(file_out, eid4, EID4_SIZE);
		free(eid4);
	}
}
Beispiel #7
0
void eid3_decrypt(s8 *file_in, s8 *file_out)
{
	u32 length;
	u8 *eid3 = _read_buffer(file_in, &length);

	if(eid3 != NULL)
	{
		eid3_decrypt_buffer(eid3);
		_write_buffer(file_out, eid3, EID3_SIZE);
		free(eid3);
	}
}
Beispiel #8
0
void eid2_generate_block(s8 *file_in, u32 blocktype, s8 *file_out)
{
	u32 length;
	u8 *eid2 = _read_buffer(file_in, &length);

	if(eid2 != NULL)
	{
		u8 *block = eid2_generate_block_buffer(eid2, blocktype);
		if(block != NULL)
		{
			_write_buffer(file_out, block, EID4_SIZE);
			free(block);
		}

		free(eid2);
	}
}
Beispiel #9
0
void eid0_decrypt(s8 *file_in, s8 *file_out)
{
	u32 length;
	u8 *eid0 = _read_buffer(file_in, &length);

	if(eid0 != NULL)
	{
		u8 section_0[EID0_SECTION_0_SIZE];
		eid0_decrypt_section_0(eid0, section_0);

		s8 fname[128];
		sprintf(fname, "%s.section_0", file_out);
		_write_buffer(fname, section_0, 0xC0);

		free(eid0);
	}
}
/*******************************************************************************
* Function Name  : MSD_ReadMultiBlock
* Description    : None
* Input          : - sector:
*				   - buffer:
*                  - NbrOfSector:
* Output         : None
* Return         : None
* Attention		 : None
*******************************************************************************/
int MSD_ReadMultiBlock(uint32_t sector, uint8_t *buffer, uint32_t NbrOfSector)
{
  uint8_t r1;
  uint32_t i;

  /* if ver = SD2.0 HC, sector need <<9 */
  if(CardInfo.CardType != CARDTYPE_SDV2HC)
  {
	 sector = sector<<9;
  }

  /* Send CMD18 : Read multi block command */
  r1 = _send_command(CMD18, sector, 0);
  if(r1 != 0x00)
  {
     return 1;
  }

  /* Start read	*/
  for(i=0; i<NbrOfSector; i++)
  {
     if(_read_buffer(buffer+i*MSD_BLOCKSIZE, MSD_BLOCKSIZE, HOLD))
     {
		 /* Send stop data transmit command - CMD12	*/
		 _send_command(CMD12, 0, 0);
		 /* chip disable and dummy byte */
		 ;
		 return 2;
     }
  }

  /* Send stop data transmit command - CMD12 */
  _send_command(CMD12, 0, 0);

  /* chip disable and dummy byte */
  _CARD_DISABLE();
  _spi_read_write(DUMMY_BYTE);

  return 0;
}
Beispiel #11
0
void read_btc(char* path, struct c_img* out_img){
	int fd, nr_blocks, i, j = 0, k, ii;
	char *big_buf;
	struct bits tmp;

	fd = _open_for_read(path);

	read(fd, &out_img->width, sizeof(int));
	read(fd, &out_img->height, sizeof(int));

	nr_blocks = out_img->width * out_img->height / (BLOCK_SIZE * BLOCK_SIZE);
	out_img->blocks = (struct block*) _alloc(nr_blocks * sizeof(struct block));

	big_buf = (char*) _alloc(nr_blocks * (2 + BLOCK_SIZE * BLOCK_SIZE / BITS_IN_BYTE));

	_read_buffer(fd, big_buf, nr_blocks * (2 + BLOCK_SIZE * BLOCK_SIZE / BITS_IN_BYTE));

	for (i=0; i<nr_blocks; i++){
		//read a and b
		out_img->blocks[i].a = big_buf[j++];
		out_img->blocks[i].b = big_buf[j++];
		//read bitplane
		k = 0;
		for (ii=0; ii<BLOCK_SIZE * BLOCK_SIZE / BITS_IN_BYTE; ii++){
			tmp = *((struct bits*)&big_buf[j++]);
			out_img->blocks[i].bitplane[k++] = tmp.bit0;
			out_img->blocks[i].bitplane[k++] = tmp.bit1;
			out_img->blocks[i].bitplane[k++] = tmp.bit2;
			out_img->blocks[i].bitplane[k++] = tmp.bit3;
			out_img->blocks[i].bitplane[k++] = tmp.bit4;
			out_img->blocks[i].bitplane[k++] = tmp.bit5;
			out_img->blocks[i].bitplane[k++] = tmp.bit6;
			out_img->blocks[i].bitplane[k++] = tmp.bit7;			
		}
	}

	free_align(big_buf);
	close(fd);
}
Beispiel #12
0
/*
* Copyright (c) 2011-2012 by ps3dev.net
* This file is released under the GPLv2.
*/

#include "types.h"
#include "keys.h"
#include "util.h"

u8 *iso_root_key = _read_buffer((s8*)"data/key",NULL);
u8 *iso_root_iv= _read_buffer((s8*)"data/iv",NULL);

u8 common_indiv_seed[INDIV_SEED_SIZE] =
{
	0x59, 0x30, 0x21, 0x45, 0xAC, 0x09, 0xB1, 0xEF, 0xE6, 0x9E, 0x9B, 0x7A, 0x25, 0xFF, 0x8F, 0x86,
    0xE9, 0xF6, 0x81, 0x4D, 0x37, 0xDE, 0x20, 0x4D, 0x29, 0x72, 0x9B, 0x84, 0x16, 0xBA, 0xED, 0xE4,
    0x22, 0x70, 0x98, 0x65, 0x7F, 0x29, 0x8C, 0xDB, 0x6A, 0x9B, 0x5E, 0x59, 0xE4, 0xA4, 0xBA, 0x2F,
    0x8E, 0x6A, 0x74, 0x0E, 0x1F, 0xC1, 0xE3, 0xE9, 0x35, 0xDD, 0xD2, 0xF6, 0x6C, 0xDE, 0xDD, 0x6B
};

u8 eid0_indiv_seed[INDIV_SEED_SIZE] =
{
	0xAB, 0xCA, 0xAD, 0x17, 0x71, 0xEF, 0xAB, 0xFC, 0x2B, 0x92, 0x12, 0x76, 0xFA, 0xC2, 0x13, 0x0C,
	0x37, 0xA6, 0xBE, 0x3F, 0xEF, 0x82, 0xC7, 0x9F, 0x3B, 0xA5, 0x73, 0x3F, 0xC3, 0x5A, 0x69, 0x0B,
	0x08, 0xB3, 0x58, 0xF9, 0x70, 0xFA, 0x16, 0xA3, 0xD2, 0xFF, 0xE2, 0x29, 0x9E, 0x84, 0x1E, 0xE4,
	0xD3, 0xDB, 0x0E, 0x0C, 0x9B, 0xAE, 0xB5, 0x1B, 0xC7, 0xDF, 0xF1, 0x04, 0x67, 0x47, 0x2F, 0x85
};

u8 eid1_indiv_seed[INDIV_SEED_SIZE] =
{
	0xB0, 0xD6, 0x55, 0x76, 0x4C, 0x3B, 0x44, 0xB3, 0x38, 0xF3, 0x2D, 0xD1, 0xD0, 0x99, 0x9B, 0x66,
/*******************************************************************************
* Function Name  : MSD_GetCardInfo
* Description    : Get SD Card Information
* Input          : None
* Output         : None
* Return         : 0:NO_ERR; TRUE: Error
* Attention		 : None
*******************************************************************************/
int MSD_GetCardInfo(PMSD_CARDINFO cardinfo)
{
    uint8_t r1;
    uint8_t CSD_Tab[16];
    uint8_t CID_Tab[16];

    /* Send CMD9, Read CSD */
    r1 = _send_command(CMD9, 0, 0xFF);
    if(r1 != 0x00)
    {
        return r1;
    }

    if(_read_buffer(CSD_Tab, 16, RELEASE))
    {
        return 1;
    }

    /* Send CMD10, Read CID */
    r1 = _send_command(CMD10, 0, 0xFF);
    if(r1 != 0x00)
    {
        return r1;
    }

    if(_read_buffer(CID_Tab, 16, RELEASE))
    {
        return 2;
    }

    /* Byte 0 */
    cardinfo->CSD.CSDStruct = (CSD_Tab[0] & 0xC0) >> 6;
    cardinfo->CSD.SysSpecVersion = (CSD_Tab[0] & 0x3C) >> 2;
    cardinfo->CSD.Reserved1 = CSD_Tab[0] & 0x03;
    /* Byte 1 */
    cardinfo->CSD.TAAC = CSD_Tab[1] ;
    /* Byte 2 */
    cardinfo->CSD.NSAC = CSD_Tab[2];
    /* Byte 3 */
    cardinfo->CSD.MaxBusClkFrec = CSD_Tab[3];
    /* Byte 4 */
    cardinfo->CSD.CardComdClasses = CSD_Tab[4] << 4;
    /* Byte 5 */
    cardinfo->CSD.CardComdClasses |= (CSD_Tab[5] & 0xF0) >> 4;
    cardinfo->CSD.RdBlockLen = CSD_Tab[5] & 0x0F;
    /* Byte 6 */
    cardinfo->CSD.PartBlockRead = (CSD_Tab[6] & 0x80) >> 7;
    cardinfo->CSD.WrBlockMisalign = (CSD_Tab[6] & 0x40) >> 6;
    cardinfo->CSD.RdBlockMisalign = (CSD_Tab[6] & 0x20) >> 5;
    cardinfo->CSD.DSRImpl = (CSD_Tab[6] & 0x10) >> 4;
    cardinfo->CSD.Reserved2 = 0; /* Reserved */
    cardinfo->CSD.DeviceSize = (CSD_Tab[6] & 0x03) << 10;
    /* Byte 7 */
    cardinfo->CSD.DeviceSize |= (CSD_Tab[7]) << 2;
    /* Byte 8 */
    cardinfo->CSD.DeviceSize |= (CSD_Tab[8] & 0xC0) >> 6;
    cardinfo->CSD.MaxRdCurrentVDDMin = (CSD_Tab[8] & 0x38) >> 3;
    cardinfo->CSD.MaxRdCurrentVDDMax = (CSD_Tab[8] & 0x07);
    /* Byte 9 */
    cardinfo->CSD.MaxWrCurrentVDDMin = (CSD_Tab[9] & 0xE0) >> 5;
    cardinfo->CSD.MaxWrCurrentVDDMax = (CSD_Tab[9] & 0x1C) >> 2;
    cardinfo->CSD.DeviceSizeMul = (CSD_Tab[9] & 0x03) << 1;
    /* Byte 10 */
    cardinfo->CSD.DeviceSizeMul |= (CSD_Tab[10] & 0x80) >> 7;
    cardinfo->CSD.EraseGrSize = (CSD_Tab[10] & 0x7C) >> 2;
    cardinfo->CSD.EraseGrMul = (CSD_Tab[10] & 0x03) << 3;
    /* Byte 11 */
    cardinfo->CSD.EraseGrMul |= (CSD_Tab[11] & 0xE0) >> 5;
    cardinfo->CSD.WrProtectGrSize = (CSD_Tab[11] & 0x1F);
    /* Byte 12 */
    cardinfo->CSD.WrProtectGrEnable = (CSD_Tab[12] & 0x80) >> 7;
    cardinfo->CSD.ManDeflECC = (CSD_Tab[12] & 0x60) >> 5;
    cardinfo->CSD.WrSpeedFact = (CSD_Tab[12] & 0x1C) >> 2;
    cardinfo->CSD.MaxWrBlockLen = (CSD_Tab[12] & 0x03) << 2;
    /* Byte 13 */
    cardinfo->CSD.MaxWrBlockLen |= (CSD_Tab[13] & 0xc0) >> 6;
    cardinfo->CSD.WriteBlockPaPartial = (CSD_Tab[13] & 0x20) >> 5;
    cardinfo->CSD.Reserved3 = 0;
    cardinfo->CSD.ContentProtectAppli = (CSD_Tab[13] & 0x01);
    /* Byte 14 */
    cardinfo->CSD.FileFormatGrouop = (CSD_Tab[14] & 0x80) >> 7;
    cardinfo->CSD.CopyFlag = (CSD_Tab[14] & 0x40) >> 6;
    cardinfo->CSD.PermWrProtect = (CSD_Tab[14] & 0x20) >> 5;
    cardinfo->CSD.TempWrProtect = (CSD_Tab[14] & 0x10) >> 4;
    cardinfo->CSD.FileFormat = (CSD_Tab[14] & 0x0C) >> 2;
    cardinfo->CSD.ECC = (CSD_Tab[14] & 0x03);
    /* Byte 15 */
    cardinfo->CSD.CSD_CRC = (CSD_Tab[15] & 0xFE) >> 1;
    cardinfo->CSD.Reserved4 = 1;

    if(cardinfo->CardType == CARDTYPE_SDV2HC)
    {
        /* Byte 7 */
        cardinfo->CSD.DeviceSize = (u16)(CSD_Tab[8]) * 256;
        /* Byte 8 */
        cardinfo->CSD.DeviceSize += CSD_Tab[9] ;
    }

    cardinfo->Capacity = cardinfo->CSD.DeviceSize * MSD_BLOCKSIZE * 1024;
    cardinfo->BlockSize = MSD_BLOCKSIZE;

    /* Byte 0 */
    cardinfo->CID.ManufacturerID = CID_Tab[0];
    /* Byte 1 */
    cardinfo->CID.OEM_AppliID = CID_Tab[1] << 8;
    /* Byte 2 */
    cardinfo->CID.OEM_AppliID |= CID_Tab[2];
    /* Byte 3 */
    cardinfo->CID.ProdName1 = CID_Tab[3] << 24;
    /* Byte 4 */
    cardinfo->CID.ProdName1 |= CID_Tab[4] << 16;
    /* Byte 5 */
    cardinfo->CID.ProdName1 |= CID_Tab[5] << 8;
    /* Byte 6 */
    cardinfo->CID.ProdName1 |= CID_Tab[6];
    /* Byte 7 */
    cardinfo->CID.ProdName2 = CID_Tab[7];
    /* Byte 8 */
    cardinfo->CID.ProdRev = CID_Tab[8];
    /* Byte 9 */
    cardinfo->CID.ProdSN = CID_Tab[9] << 24;
    /* Byte 10 */
    cardinfo->CID.ProdSN |= CID_Tab[10] << 16;
    /* Byte 11 */
    cardinfo->CID.ProdSN |= CID_Tab[11] << 8;
    /* Byte 12 */
    cardinfo->CID.ProdSN |= CID_Tab[12];
    /* Byte 13 */
    cardinfo->CID.Reserved1 |= (CID_Tab[13] & 0xF0) >> 4;
    /* Byte 14 */
    cardinfo->CID.ManufactDate = (CID_Tab[13] & 0x0F) << 8;
    /* Byte 15 */
    cardinfo->CID.ManufactDate |= CID_Tab[14];
    /* Byte 16 */
    cardinfo->CID.CID_CRC = (CID_Tab[15] & 0xFE) >> 1;
    cardinfo->CID.Reserved2 = 1;

    return 0;
}