Tuesday, February 23, 2021

The journey for updating Realtek NIC MAC

Realtek Network Interface Controller (NIC, also known as a Network Interface Card) is widely used in PC. Usually an EEPROM would be connected to the RTL NIC chip to provide programmable storage for saving some network related setting, such as MAC address. IP address is bound with the MAC. So if multiple NIC has same MAC, it would be a disaster for switch/bridge/router to correctly forward IP packet.

On Linux and Windows, it is possible to change the MAC address temporarily by using some command such as ifconfig, but the setting won't persist during reboot. It is also possible to override the MAC address with a saved file or value stored in Registry table and keep the change permanently. However, this change can also get lost if the card is moved to a different setup, or the OS gets reinstalled.

So to permanently change the MAC, will need to reprogram the EEPROM. There is an Open Source tools, rtl8168-eeprom, supposed can be used to update EEPROM. However, several issues to get it working:

1) open('/dev/mem') may fail, due to a new kernel feature 'KERNEL_LOCKDOWN'.

2) Rebuild kernel from source. After build and install, it cannot boot the new kernel with error

/boot/vmlinuz-x.y.z-generic has invalid signature

This is due to for secure boot, customer kernel has to be signed. Refer to this.

3) Tried to build kernel with CONFIG_SECURITY_LOCKDOWN_LSM disabled, will run into problem during kernel_config, as KERNEL_LOCKDOWN is forced on due to config policy setting

4) Build old kernel 4.4 from source for Ubuntu 20.04 (Focal Fossa) doesn't work very well, as the kernel version is too low which cause build problem.

5) So the way to run the tools is install kernel 4.4 deb package from LaunchPad. However, the tools cannot properly access RTL8168 EEPROM as it failed to read back the ID from EEPROM.

Ethtool has option '-m' for dump module EEPROM, '-e' for dump EEPROM, and '-E' for update MAC. However, running ethtool with these option may get  'Operation Not Permitted' error. Run 'ethtool -i devname' may get 'support-eeprom-access: no'. So the NIC driver does not support EEPROM access.

As a side note, Realtek might have released some tools for this purpose, which can run on Windows, Linux, and UEFI shell. You can search for 'RTNicPG' from Internet. I'm able to find several links but none of them from Realtek official website. Maybe it got leaked out as an internal used tools.

Sunday, February 7, 2021

Update build.gradle for build native code with NDK

Because of Android Studio/Gradle keeps evolution, and the special need of Android NDK's configuration, sometimes it is a little difficult to get things working smoothly.

There is some references like:

https://riptutorial.com/android/example/13985/enable-experimental-ndk-plugin-support-for-gradle-and-androidstudio

https://stackoverflow.com/questions/16667903/android-studio-gradle-and-ndk

https://android-developers.googleblog.com/2020/02/native-dependencies-in-android-studio-40.html

Will see some mismatch between above posts, or things not working with Android Studio, such as with Android Studio 4.1.2, Gradle model 6.5, the ndk setting in above first link will lead to weird error. The 2nd link from stackoverflow has the correct setting, i.e. ndk setting has to be put under defaultConfig, specially the abiFilters setting. As explained in the 3rd link, when deal with native code, frequently may need to link with prebuilt lib, which may only support one special ABI such as armeabi-v7a, but not all others such as 64 bit or x86. Here is an example app build.gradle file for android-eye:

apply plugin: 'com.android.application'

android {
compileSdkVersion 14
buildToolsVersion "30.0.3"
ndkVersion "17.2.4988734"

defaultConfig {
applicationId "teaonly.droideye"
targetSdkVersion 14
minSdkVersion="14"
ndk {
abiFilters 'armeabi-v7a'
// 64-bit support requires an Android API level higher than 19; Namely 21 and higher
//abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
//cppFlags.add("-O2 -Werror -Wall")
//ldLibs.addAll(['log', 'z', 'ld'])
stl = "c++_static"
}

}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
externalNativeBuild {
ndkBuild {
path file('src/main/jni/Android.mk')
}
}
}

dependencies {
implementation files('libs/java_websocket.jar')
}

Building under Android Studio may hide the detail of the build, which makes it difficult to root cause when something goes wrong.

Beside gradle related setting, sometimes developer might get sysroot setting issue. Such as compiling x264 with NDK (specially old version) sometimes need some tweak, such as sysroot setting. Refer to SO1, SO2, and BuildSystemMaintainers for latest new NDK. For old NDK, at compile time sysroot is $NDK/sysroot, and at link time sysroot points to $NDK/platforms/android-$API/arch-$ARCH. One way is refer to standalone_toolchain, run $NDK/build/tools/make_standalone_toolchain.py --arch arm --api 14 --install-dir /tmp/my_toolchain to create a standalone toolchain. May need to specify CFLAGS=-D__ANDROID_API__=$API as mentioned here. The other possible way is refer to this: Using clang instead of gcc also works, e.g. for autotools-based projects CC=arm-linux-androideabi-clang CXX=arm-linux-androideabi-clang++ ./configure --cross-prefix=arm-linux-androideabi- ... works without disabling unified headers and without specifying __ANDROID_API__. 

With new version NDK,  'Application.mk' has to be provided. Also, NDK will assume the 'Application.mk' and 'Android.mk' are all located under a 'jni' sub-folder. If that is not the case, then user will have to specify the path of 'Application.mk' while invoking 'ndk-build' script, by setting NDK_APPLICATION_MK, and in 'Application.mk', user has to specify the path of 'Android.mk' by set 'APP_BUILD_SCRIPT'. Refer to https://developer.android.com/ndk/guides/ndk-build for more detail.

Saturday, January 9, 2021

学习TensorFlow - 设置Python环境

 TensorFlow的网址:https://www.tensorflow.org,Wikipedia介绍,是由Google开发,后来捐献成开源项目,提供machine-learning的API和库支持。虽然TensorFlow的core是用C++开发的,TensorFlow同时提供多种语言如C++, Python, Java等的外包(wrapper),而Python目前还是ML最流行的开发语言。TensorFlow适用于底层建模,上层可用Keras等module来简化。

TensorFlow的教程很多,我也还在学习。设置Python环境是第一步,很多教程提供了不同的设置却没有明确的解释为什么。Python有很多不同的版本,其中2.x和3.x差别很大,另外Python有很多软件包,这些包也有不同版本。一个复杂的项目可能用到特定版本软件包,如果把多个项目安装在同一环境下,可能会遇到软件包版本冲突,所以简单的解决办法就是隔离每个项目的运行环境。为了达到这个目的,可以用Python的env包来设置虚拟环境,或安装Docker container,或安装Anaconda/MiniConda并建立虚拟环境。Python虚拟环境的实质就是把所有的软件包和工具安装在单独的目录下来避免冲突。所有每增加一个环境都要占用更多的硬盘空间。实际上我之前在“Get fun with google assistant”提到的Google Assistant虚拟环境可以与TensorFlow共用。

有的教程要安装PyCharm, 其实完全没必要。PyCharm是Jetbrains开发的Python集成开发环境(IDE),是个很不错的软件(Google的Android Studio是基于Jetbrains开发的IntelliJ IDEA, JetBrains的TeamCity是仅次于Jenkins的Continous Integration Tool),用Visual Studio Code开发也很适手。

注意TensorFlow 2.0有很多改变不同于1.0,所以遇到AttributeError: module 'tensorflow' has no attribute 'placeholder',请参照https://www.tensorflow.org/guide/migrate,简单的办法是把

import tensorflow as tf
改成:
import tensorflow.compat.v1 as tf
tf
.disable_v2_behavior()
@ Mar 11, 2023
Windows上设置miniconda有时有点very frustrate,主要是安装过程可能没有设置系统路径(System Environment
 Variable: PATH),所以当用户在自己打开的cmd console下运行conda command时,只会得到找不到命令的错误。
所以最好把conda的path加到Environment Variable Path setting, 注意conda.exe通常是在Scripts子目录下。
VSCode通常无法detect到conda/env setting,如果conda executable不在系统路径里。关于在VS Code中设置
Python Environment,可参见: https://code.visualstudio.com/docs/python/environments

Tuesday, December 29, 2020

github will cease support using username/password for login

Github will stop supporting username/password for accessing:
Basic authentication using a password to Git is deprecated and will soon no longer work. 
Visit https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more
 information around suggested workarounds and removal dates.
Beginning August 13, 2021, we will no longer accept account passwords when authenticating Git operations
 on GitHub.com. For developers, if you are using a password to authenticate Git operations with GitHub.com
 today, you must begin using a personal access token over HTTPS (recommended) or SSH key by August 13, 
2021, to avoid disruption.
I'm not very familiar with how the token can be used. I assume that is for HTTPS. As I know, for daily pull/
push, SSH key is commonly used. You can follow instruction to generate the RSA key pair, and upload the
public key to Github.  If run into issue, refer to troubleshoot common SSH Problems, especially
Reviewing your SSH keys
If run 'ssh-add -l -E sha256' gives 'The agent has no identities', probably you haven't run ssh-add to add the
 key to ssh-agent. You can run: ssh-add -E sha256, then rerun with '-l'. 
For your exist local repository, may need update the remote url as this:
Check remote url: git remote -v
Update remote url: git remote set-url origin git@github.com:your_github_username/your_proj.git
Note, it is 'git@' but not 'your_username@'.

Tuesday, December 8, 2020

Chicken & egg: Google Account Verify code via SMS

 Recently Google announced policy update which mentioned an email account may get deactivated if not used for a long time. I had registered an email address with a personal name and I don't want to lose it. But I haven't used it for a while since my previous old computer retired. So I tried to log into the account with my new computer. The password still works. Everything looks OK, until when I tried to add the account to my Thunderbird email app, it redirected me to Google website and asked for 6 digital verification code which supposed to be sent to my phone as text message (SMS). However, for some reason, I cannot get the code no matter how many times I tried. It worked before, that's how the phone number got bound with my Gmail account. I got to Google Account management site and did see besides the phone number, I have recovery email and secret question set as recovery method. But, when I trying to change or remove the phone, it prompted me again with code sending to my phone.

Select "More way to verify" doesn't help, it will always go back to the same page: get a verification code with the phone text message. I googled, and found others encountering the same problem, with no solution. Yes, I tried recovery Google Account. Since I have the right current password, it always says recovery success. But, I still cannot get the code, and I cannot change the phone number, and it does not prompt for other way such as phone call as verify method. It's so frustrate, and eventually, I saw this page from Google's FAQ at https://support.google.com/accounts/answer/183723?hl=en&ref_topic=3382296:

Can't change recovery info

If there’s something different about how you’re signing in, you might not have the option to change your recovery info. You can try again:

  • From a device you normally use to sign in.
  • From a location where you normally sign in.
  • The following week from the device that you’re currently using.

So my best luck is: try after a week, with same computer. Hopefully it will work.

Update: I was able to update the setting after couple days later.