CODE2

2019年6月5日 星期三

【UEFI】Simple Pointer Protocol

We have an issue with i2C touch pad in factory, so I write a efi shell application for factory to check the movement of touch pad.

Protocol : EFI_SIMPLE_POINTER_PROTOCOL

The definition of EFI_SIMPLE_POINTER_PROTOCOL in UEFI spec v2.6




















EFI_SIMPLE_POINTER_PROTOCOL.GetState():





















First, we define the GUID of SIMPLE PROTOCOL
#include <efi.h>
#include <efilib.h>

#define EFI_SIMPLE_POINTER_PROTOCOL_GUID \
 {0x31878c87, 0xb75,0x11d5,0x9a,0x4f,0x00,0x90,0x27,0x3f,0xc1,0x4d}

Then declare system table, boot service, runtime service
EFI_GUID efi_simple_pointer_protocol_guid = EFI_SIMPLE_POINTER_PROTOCOL_GUID;
EFI_SYSTEM_TABLE *my_system_table;
EFI_BOOT_SERVICES *my_boot_services;
EFI_RUNTIME_SERVICES *my_runtime_services;
EFI_SIMPLE_POINTER_PROTOCOL *SimplePointer;
EFI_SIMPLE_POINTER_STATE *State;

The entry:
EFI_STATUS Simple_protocol(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *my_system_table)
{ 
 EFI_STATUS Status; 

 InitializeLib (ImageHandle, my_system_table);
 my_boot_services=my_system_table->BootServices;
 my_runtime_services=my_system_table->RuntimeServices;
 
 Status = my_boot_services->LocateProtocol(
  &efi_simple_pointer_protocol_guid,
  NULL,
  (VOID**)&SimplePointer);
 if (EFI_ERROR(Status)) {
  Print(L"Status: %r\n",Status);
  return Status;  
 } 
 
 Status = SimplePointer->GetState(SimplePointer,State);
  
 if (EFI_ERROR(Status)) {
  // Print(L"waiting \n",Status);
   return Status;  
        } else {
  Print(L"X: %d\n",State->RelativeMovementX);
  Print(L"Y %d\n",State->RelativeMovementY);
  return 0;
  }
  
 }
 return 0; 
}

The sample efi application(Google Drive):
http://bit.ly/2WGSNKL
usage:
















1.if no parameter, the application will wait until touch the touch pad
fs5:\>Simple_protocol.efi

2. Add time parameter(sec)
fs5:\>Simple_protocol.efi 15
In 15 sec, nobody touch will show nobody touch, otherwise, screen will show the movement.

2018年6月28日 星期四

【WPF】ScrollViewer can't scroll?

original xmal:
                    <ScrollViewer Canvas.Right="20" Canvas.Top="10" CanContentScroll="True">
                        <DataGrid  Name="data" Canvas.Right="50" Canvas.Top="0" IsReadOnly="True">
                            <DataGrid.Columns>
                                <DataGridTextColumn Header="CLASS" Binding="{Binding Path=Class}"/>
                                <DataGridTextColumn Header="ENTRY NO." Binding="{Binding Path=EntryNo}"/>
                                <DataGridTextColumn Header="FEE" Binding="{Binding Path=fee}"/>
                            </DataGrid.Columns>
                        </DataGrid>
                    </ScrollViewer>

2018年6月21日 星期四

【C#】Duplicate Object

日前因工作需要做了一個不開VisualeBios build code的Tool, 因需要一次build好幾個project,所以在ui上要能有複製物件的功能,才不用在那邊拉元件拉個半死 成果如下:
複製方法如以下連結
參考來源: https://stackoverflow.com/questions/4544657/duplicate-group-box

2017年11月1日 星期三

【C#】Set/Get firmware environment variable

Use Set/Get FirmwareEnvironmentVariable function with C#, we need load DLL .

        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]

        internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,

ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);



        [DllImport("kernel32.dll", ExactSpelling = true)]

        internal static extern IntPtr GetCurrentProcess();



        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]

        internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr

        phtok);



        [DllImport("advapi32.dll", SetLastError = true)]

        internal static extern bool LookupPrivilegeValue(string host, string name,

        ref long pluid);



        [StructLayout(LayoutKind.Sequential, Pack = 1)]

        internal struct TokPriv1Luid

        {

            public int Count;

            public long Luid;

            public int Attr;

        }



        internal const int SE_PRIVILEGE_ENABLED = 0x00000002;

        internal const int TOKEN_QUERY = 0x00000008;

        internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;

        internal const string SE_SYSTEM_ENVIRONMENT_NAME = "SeSystemEnvironmentPrivilege"; //http://msdn.microsoft.com/en-us/library/bb530716(VS.85).aspx




        [DllImport("kernel32.dll", EntryPoint = "SetFirmwareEnvironmentVariableEx", SetLastError = true)]

        [return: MarshalAs(UnmanagedType.Bool)]

        static unsafe extern bool SetFirmwareEnvironmentVariableEx(string lpName, string lpGuid, IntPtr pValue, int nSize, int Attributes);

        [DllImport("kernel32.dll", SetLastError = true)]

        static extern UInt32 GetFirmwareEnvironmentVariable(string lpName, string lpGuid, UINT8[] pBuffer, int nSize);

In this step we declare function:
        public static void SetVariable(string GUID, string NAME, int ATTRIBUTES, byte VALUE)

        {

            byte[] Data = new byte[1]; //size

            Data[0] = VALUE; // value

            IntPtr unmanagedPointer = IntPtr.Zero;

            unmanagedPointer = Marshal.AllocHGlobal(Data.Length);

            Marshal.Copy(Data, 0, unmanagedPointer, Data.Length);



            var ResultsANSII = SetFirmwareEnvironmentVariableEx(NAME, GUID, unmanagedPointer, sizeof(byte) * Data.Length, ATTRIBUTES);

            Marshal.FreeHGlobal(unmanagedPointer);

        }

Get Variable and Delete Variable:
        public static void SetVariable(string GUID, string NAME, int ATTRIBUTES, List DataList)
        {
            byte[] Data = new byte[DataList.Count];
            int i = 0;
            foreach (byte by in DataList)
            {
                Data[i] = by;
                i++;
            }
            IntPtr unmanagedPointer = IntPtr.Zero;
            unmanagedPointer = Marshal.AllocHGlobal(Data.Length);
            Marshal.Copy(Data, 0, unmanagedPointer, Data.Length);

            var ResultsANSII = SetFirmwareEnvironmentVariableEx(NAME, GUID, unmanagedPointer, sizeof(byte) * Data.Length, ATTRIBUTES);
            Marshal.FreeHGlobal(unmanagedPointer);
        }
        public static void DelVariable(string GUID, string NAME)
        {
            byte[] Data = new byte[1];
            IntPtr unmanagedPointer = IntPtr.Zero;
            unmanagedPointer = Marshal.AllocHGlobal(Data.Length);
            Marshal.Copy(Data, 0, unmanagedPointer, Data.Length);

            var ResultsANSII = SetFirmwareEnvironmentVariableEx(NAME, GUID, unmanagedPointer, 0, 7);
            Marshal.FreeHGlobal(unmanagedPointer);
        }

Reference :https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-setfirmwareenvironmentvariableexa

update:
I made a small tool to get/set/del variable with those functions by C#.
download link(Google Drive):
https://bit.ly/2HNjJ35
Icon made by Smashicons from www.flaticon.com




Here's the source code on github



2016年11月11日 星期五

【UEFI】EFI_SIMPLE_TEXT_INPUT_PROTOCOL

EFI_SIMPLE_TEXT_INPUT_PROTOCOL

當我們需要輸入任何字元時,我們必須使用此protocol

範例如下:


#include <efi.h>
#include <efilib.h>

EFI_STATUS
ConsoleInput(
    IN EFI_HANDLE           ImageHandle,
    IN EFI_SYSTEM_TABLE     *SystemTable
    )
{
EFI_INPUT_KEY Key;
int count=0;
UINTN EventIndex,K;
CHAR16 *InputNumber={0};
InitializeLib (ImageHandle, SystemTable);
Print(L"\n Enter the number:\n");

do{
    count++;
    BS->WaitForEvent (1 , &ST-> ConIn-> WaitForKey, &EventIndex);
ST->ConIn->ReadKeyStroke(ST -> ConIn, &Key);
InputNumber[count]=Key.UnicodeChar;
Print(L"%c",Key.UnicodeChar);

}
while(InputNumber[count]!= '\r');
InputNumber[count]= '\0';
K=Atoi(InputNumber);
Print(L"\nNumber=%d \n",K);

return EFI_SUCCESS;
}


如此一來在shell底下執行則可由鍵盤輸入數字並顯示在螢幕上!