blob: 00e9567aba22da12b6ceee75a7f6f1f058ef09ab [file] [log] [blame]
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001#include "OverrideLog.h"
2#include "spdhelper.h"
3#include "config.h"
4
5void SpdHelper::setPatchAsBad()
6{
7 getInstance().setPatchAsBadImpl();
8}
9
10void SpdHelper::incErrorCount()
11{
12 getInstance().incErrorCountImpl();
13}
14
15bool SpdHelper::isPatchBad(UINT8* prm, UINT32 len)
16{
17 return getInstance().isPatchBadImpl(prm, len);
18}
19
20bool SpdHelper::isSpdDebug()
21{
22 bool b = getInstance().isSpdDebugImpl();
23 ALOGD("%s SpdDebug is %s", __func__, (b ? "TRUE" : "FALSE"));
24 return b;
25}
26
27void SpdHelper::incErrorCountImpl()
28{
29 if (++mErrorCount >= mMaxErrorCount)
30 {
31 setPatchAsBadImpl();
32 }
33}
34
35void SpdHelper::setPatchAsBadImpl()
36{
37 mIsPatchBad = true;
38}
39
40inline const char * toHex(UINT8 b)
41{
42 static char hex[] = "0123456789ABCDEF";
43 static char c[3];
44 c[0] = hex[((b >> 4) & 0x0F)];
45 c[1] = hex[((b >> 0) & 0x0F)];
46 c[2] = '\0';
47 return &c[0];
48}
49
50bool SpdHelper::isPatchBadImpl(UINT8* prm, UINT32 len)
51{
52 string strNew;
53
54 // Get the patch ID from the prm data.
55 for (int i = 0; i < 8 && i < len; ++i)
56 strNew.append(toHex(*prm++));
57
58 // If it is not the same patch as before, then reset things.
59 if ( strNew != mPatchId )
60 {
61 mPatchId = strNew;
62 mErrorCount = 0;
63 mIsPatchBad = false;
64 }
65
66 // Otherwise the 'mIsPatchBad' will tell if its bad or not.
67 ALOGD("%s '%s' (%d) is %sa known bad patch file", __func__, mPatchId.c_str(), mErrorCount, (mIsPatchBad ? "" : "not "));
68
69 return mIsPatchBad;
70}
71
72SpdHelper& SpdHelper::getInstance()
73{
74 static SpdHelper* theInstance = NULL;
75 if (theInstance == NULL)
76 theInstance= new SpdHelper;
77 return *theInstance;
78}
79
80SpdHelper::SpdHelper()
81{
82 mErrorCount = 0;
83 mPatchId.erase();
84 if(!GetNumValue((char*)NAME_SPD_MAXRETRYCOUNT, &mMaxErrorCount, sizeof(mMaxErrorCount)))
85 mMaxErrorCount = DEFAULT_SPD_MAXRETRYCOUNT;
86 if (!GetNumValue((char*)NAME_SPD_DEBUG, &mSpdDebug, sizeof(mSpdDebug)))
87 mSpdDebug = false;
88}