Cleaned-up i2c code, fixed wrong comments

This commit is contained in:
Aurora 2016-03-20 01:42:23 +01:00
parent d522481d44
commit 1698c8afe5
6 changed files with 20 additions and 33 deletions

View File

@ -314,12 +314,13 @@ void writeFirm(u8 *inbuf, u32 firm, u32 size){
sdmmc_nand_writesectors(offset / 0x200, size / 0x200, inbuf); sdmmc_nand_writesectors(offset / 0x200, size / 0x200, inbuf);
} }
//Setup keyslot 0x11 for key sector de/encryption
void setupKeyslot0x11(const u8 *otp){ void setupKeyslot0x11(const u8 *otp){
u8 shasum[0x20]; u8 shasum[0x20];
u8 keyX[0x10]; u8 keyX[0x10];
u8 keyY[0x10]; u8 keyY[0x10];
//Set keyX and keyY for key sector encryption //Set keyX and keyY
sha(shasum, otp, 0x90, SHA_256_MODE); sha(shasum, otp, 0x90, SHA_256_MODE);
memcpy(keyX, shasum, 0x10); memcpy(keyX, shasum, 0x10);
memcpy(keyY, shasum + 0x10, 0x10); memcpy(keyY, shasum + 0x10, 0x10);
@ -327,7 +328,7 @@ void setupKeyslot0x11(const u8 *otp){
aes_setkey(0x11, keyY, AES_KEYY, AES_INPUT_BE | AES_INPUT_NORMAL); aes_setkey(0x11, keyY, AES_KEYY, AES_INPUT_BE | AES_INPUT_NORMAL);
} }
//Get Nand CTR key //Generate and encrypt an A9LH key sector
void generateSector(u8 *keySector){ void generateSector(u8 *keySector){
//Inject A9LH key2 //Inject A9LH key2
memcpy(keySector + 0x10, a9lhKey2, 0x10); memcpy(keySector + 0x10, a9lhKey2, 0x10);
@ -338,7 +339,7 @@ void generateSector(u8 *keySector){
aes(keySector + (0x10 * i), keySector + (0x10 * i), 1, NULL, AES_ECB_ENCRYPT_MODE, 0); aes(keySector + (0x10 * i), keySector + (0x10 * i), 1, NULL, AES_ECB_ENCRYPT_MODE, 0);
} }
//Get Nand CTR key //Test the OTP to be correct by verifying key2
u32 testOtp(u32 mode){ u32 testOtp(u32 mode){
//Read keysector from NAND //Read keysector from NAND
sdmmc_nand_readsectors(0x96, 0x1, (vu8 *)0x24500000); sdmmc_nand_readsectors(0x96, 0x1, (vu8 *)0x24500000);
@ -352,6 +353,7 @@ u32 testOtp(u32 mode){
return 1; return 1;
} }
//Check SHA256 hash
u32 verifyHash(const void *data, u32 size, const u8 *hash){ u32 verifyHash(const void *data, u32 size, const u8 *hash){
u8 shasum[0x20]; u8 shasum[0x20];
sha(shasum, data, size, SHA_256_MODE); sha(shasum, data, size, SHA_256_MODE);

View File

@ -1,3 +1,4 @@
#pragma once #pragma once
#include <stdbool.h>
#include "../../types.h" #include "../../types.h"

View File

@ -402,7 +402,6 @@ int sdmmc_sdcard_init()
return result | SD_Init(); return result | SD_Init();
} }
int sdmmc_get_cid( int isNand, uint32_t *info) int sdmmc_get_cid( int isNand, uint32_t *info)
{ {
struct mmcdevice *device; struct mmcdevice *device;
@ -443,4 +442,4 @@ int sdmmc_get_cid( int isNand, uint32_t *info)
} }
return 0; return 0;
} }

View File

@ -10,11 +10,11 @@ static const struct { u8 bus_id, reg_addr; } dev_data[] = {
{2, 0xA4}, {2, 0x9A}, {2, 0xA0}, {2, 0xA4}, {2, 0x9A}, {2, 0xA0},
}; };
inline u8 i2cGetDeviceBusId(u8 device_id) { static inline u8 i2cGetDeviceBusId(u8 device_id) {
return dev_data[device_id].bus_id; return dev_data[device_id].bus_id;
} }
inline u8 i2cGetDeviceRegAddr(u8 device_id) { static inline u8 i2cGetDeviceRegAddr(u8 device_id) {
return dev_data[device_id].reg_addr; return dev_data[device_id].reg_addr;
} }
@ -26,7 +26,7 @@ static vu8* reg_data_addrs[] = {
(vu8*)(I2C3_REG_OFF + I2C_REG_DATA), (vu8*)(I2C3_REG_OFF + I2C_REG_DATA),
}; };
inline vu8* i2cGetDataReg(u8 bus_id) { static inline vu8* i2cGetDataReg(u8 bus_id) {
return reg_data_addrs[bus_id]; return reg_data_addrs[bus_id];
} }
@ -38,22 +38,22 @@ static vu8* reg_cnt_addrs[] = {
(vu8*)(I2C3_REG_OFF + I2C_REG_CNT), (vu8*)(I2C3_REG_OFF + I2C_REG_CNT),
}; };
inline vu8* i2cGetCntReg(u8 bus_id) { static inline vu8* i2cGetCntReg(u8 bus_id) {
return reg_cnt_addrs[bus_id]; return reg_cnt_addrs[bus_id];
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
inline void i2cWaitBusy(u8 bus_id) { static inline void i2cWaitBusy(u8 bus_id) {
while (*i2cGetCntReg(bus_id) & 0x80); while (*i2cGetCntReg(bus_id) & 0x80);
} }
inline bool i2cGetResult(u8 bus_id) { static inline u32 i2cGetResult(u8 bus_id) {
i2cWaitBusy(bus_id); i2cWaitBusy(bus_id);
return (*i2cGetCntReg(bus_id) >> 4) & 1; return (*i2cGetCntReg(bus_id) >> 4) & 1;
} }
void i2cStop(u8 bus_id, u8 arg0) { static void i2cStop(u8 bus_id, u8 arg0) {
*i2cGetCntReg(bus_id) = (arg0 << 5) | 0xC0; *i2cGetCntReg(bus_id) = (arg0 << 5) | 0xC0;
i2cWaitBusy(bus_id); i2cWaitBusy(bus_id);
*i2cGetCntReg(bus_id) = 0xC5; *i2cGetCntReg(bus_id) = 0xC5;
@ -61,14 +61,14 @@ void i2cStop(u8 bus_id, u8 arg0) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
bool i2cSelectDevice(u8 bus_id, u8 dev_reg) { static u32 i2cSelectDevice(u8 bus_id, u8 dev_reg) {
i2cWaitBusy(bus_id); i2cWaitBusy(bus_id);
*i2cGetDataReg(bus_id) = dev_reg; *i2cGetDataReg(bus_id) = dev_reg;
*i2cGetCntReg(bus_id) = 0xC2; *i2cGetCntReg(bus_id) = 0xC2;
return i2cGetResult(bus_id); return i2cGetResult(bus_id);
} }
bool i2cSelectRegister(u8 bus_id, u8 reg) { static u32 i2cSelectRegister(u8 bus_id, u8 reg) {
i2cWaitBusy(bus_id); i2cWaitBusy(bus_id);
*i2cGetDataReg(bus_id) = reg; *i2cGetDataReg(bus_id) = reg;
*i2cGetCntReg(bus_id) = 0xC0; *i2cGetCntReg(bus_id) = 0xC0;
@ -77,7 +77,7 @@ bool i2cSelectRegister(u8 bus_id, u8 reg) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
bool i2cWriteRegister(u8 dev_id, u8 reg, u8 data) { u32 i2cWriteRegister(u8 dev_id, u8 reg, u8 data) {
u8 bus_id = i2cGetDeviceBusId(dev_id); u8 bus_id = i2cGetDeviceBusId(dev_id);
u8 dev_addr = i2cGetDeviceRegAddr(dev_id); u8 dev_addr = i2cGetDeviceRegAddr(dev_id);
@ -88,11 +88,11 @@ bool i2cWriteRegister(u8 dev_id, u8 reg, u8 data) {
*i2cGetCntReg(bus_id) = 0xC1; *i2cGetCntReg(bus_id) = 0xC1;
i2cStop(bus_id, 0); i2cStop(bus_id, 0);
if (i2cGetResult(bus_id)) if (i2cGetResult(bus_id))
return true; return 1;
} }
*i2cGetCntReg(bus_id) = 0xC5; *i2cGetCntReg(bus_id) = 0xC5;
i2cWaitBusy(bus_id); i2cWaitBusy(bus_id);
} }
return false; return 0;
} }

View File

@ -15,18 +15,4 @@
#define I2C_DEV_GYRO 10 #define I2C_DEV_GYRO 10
#define I2C_DEV_IR 13 #define I2C_DEV_IR 13
u8 i2cGetDeviceBusId(u8 device_id); u32 i2cWriteRegister(u8 dev_id, u8 reg, u8 data);
u8 i2cGetDeviceRegAddr(u8 device_id);
vu8* i2cGetDataReg(u8 bus_id);
vu8* i2cGetCntReg(u8 bus_id);
void i2cWaitBusy(u8 bus_id);
bool i2cGetResult(u8 bus_id);
u8 i2cGetData(u8 bus_id);
void i2cStop(u8 bus_id, u8 arg0);
bool i2cSelectDevice(u8 bus_id, u8 dev_reg);
bool i2cSelectRegister(u8 bus_id, u8 reg);
bool i2cWriteRegister(u8 dev_id, u8 reg, u8 data);

View File

@ -6,7 +6,6 @@
#pragma once #pragma once
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>